How to bind XML file in GridView [Resolved]

Posted by Thiru under ASP.NET on 10/18/2011 | Points: 10 | Views : 7528 | Status : [Member] | Replies : 13
Hi Friends,

While i try to bind an xml file in gridview - its getting an error:
DataBinding: 'System.Web.UI.WebControls.XmlDataSourceNodeDescriptor' does not contain a property with the name 'date'.

note: i can able to bind any other xml file
but i need to bind the xml file which is attached here with.


Please try to use the code attached here with (the XML file and Gridview code )


CodeBehind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If IsPostBack = False Then
Dim ds As DataSet = New DataSet
ds.ReadXml(Server.MapPath("Events.xml"))
Session("xmldata") = ds.Tables
Call bind()
End If
End Sub

Public Function bind()
GVxml.DataSource = Session("xmldata")
GVxml.DataBind()
Return Nothing
End Function





Responses

Posted by: Ndebata on: 10/20/2011 [Member] Starter | Points: 50

Up
0
Down

Resolved
Trying to attach it one more time
 Download source file

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/18/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Thriu,

Either you have to change your xml in to keep every thing at one level something like this in case you want to use ReadXML() of dataset.
<Events>

<event>
<day>4</day>
<month>7</month>
<year>2010</year>
<title><![CDATA[<head>Fireworks</head>]]></title>
<when><![CDATA[<body>5 PM</body>]]></when>
<where><![CDATA[<head>Joe's house</head>]]></where>
<description><![CDATA[<head>WOW!!!</head><body>It's gonna be awesome.<br/> You will notice that this infobox window scrolls to fit as much content as you want to put into it. <br/> The scroll bar on the right will only appear if there is more content in the <description> tag, inside the Events.xml file, than the visible area can hold.</body>]]></description>
<image><![CDATA[assets/images/work.png]]></image>
<url><![CDATA[http://www.flashxml.net]]></url>
</event>
<Events>


Else you can use LINQ to read those xml and build a collection of an object then assign it to gridview.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Godly77 on: 10/19/2011 [Member] Starter | Points: 25

Up
0
Down
Asp. net 


Dim DSet As New System.Data.DataSet

DSet.ReadXml(Server.MapPath("XMLFile.xml"))

GridView1.DataSource = DSet.Tables(0)
GridView1.DataBind()


XML F ile
<data>

<emp>

<Name>Godly Mathew</Name>
<age>28</age>

</emp>
<emp>

<Name>Sumith Koshy</Name>
<age>27</age>

</emp>
<emp>

<Name>Franklin Benedict</Name>
<age>31</age>

</emp>
</data>


Godly Mathew

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/19/2011 [Member] Starter | Points: 25

Up
0
Down
Dear Ndebata,

Thanks for your mail.
I tried to write the xml file as:
<event>

<day>15</day>
<month>10</month>
<year>2011</year>
<title><![CDATA[<head>Lunch</head>]]></title>
<when><![CDATA[<body>1 PM</body>]]></when>
<where><![CDATA[<head>Corner Cafe</head>]]></where>
<description><![CDATA[<head>Download the new Calendar FX</head><body>For FREE!<br/> You will notice that this infobox window scrolls to fit as much content as you want to put into it. <br/> The scroll bar on the right will only appear if there is more content in the <description> tag, inside the Events.xml file, than the visible area can hold.</body>]]></description>
<photo image="assets/images/eat.png" url = "http://www.flashxml.net" target="_blank"/>
</event>

instead of:
<date day="15" month="10" year="2011">

<event>
<title><![CDATA[<head>Work</head>]]></title>
<when><![CDATA[<body>8 AM</body>]]></when>
<where><![CDATA[<head>FlashXML.net HQ</head>]]></where>
<description><![CDATA[<head>Hello!<body>Welcome to the new Calendar FX.<br/> You will notice that this infobox window scrolls to fit as much content as you want to put into it. <br/> The scroll bar on the right will only appear if there is more content in the <description> tag, inside the Events.xml file, than the visible area can hold.</body>]]></description>
</event>
</date>


Actually i am using this xml file as backend for a flash file.
Flash file is not working. if i change the format of xml.

so, i need to have the same xml format as:
<date day="15" month="10" year="2011">

<event>
<title><![CDATA[<head>Work</head>]]></title>
<when><![CDATA[<body>8 AM</body>]]></when>
<where><![CDATA[<head>FlashXML.net HQ</head>]]></where>
<description><![CDATA[<head>Hello!<body>Welcome to the new Calendar FX.<br/> You will notice that this infobox window scrolls to fit as much content as you want to put into it. <br/> The scroll bar on the right will only appear if there is more content in the <description> tag, inside the Events.xml file, than the visible area can hold.</body>]]></description>
</event>
</date>



--- here i standing still :-(
if you have any other suggestion please reply.

regards,
Thiru.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Ndebata,
Now i can able to create new events and is working fine with flash file too.

But in need of two things.

I can able to Add new data in xml file - but let me know how to update (how to append new data with existing data)

And i am facing problem while loading that XML file in GridView.
(note: its getting me error only if i load the attached XML file)
Error:
DataBinding: 'System.Data.DataTable' does not contain a property with the name 'date'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Web.HttpException: DataBinding: 'System.Data.DataTable' does not contain a property with the name 'date'.

Source Error:

Line 19: <asp:TemplateField HeaderText="Event Date">
Line 20: <ItemTemplate >
Line 21: <asp:Label ID="lblEdtp" runat="server" Text='<%# bind("date")%>'></asp:Label>
Line 22: </ItemTemplate>
Line 23: <EditItemTemplate>


It will be great if you get me a solution for the above two questions - please.

Regards,
Thiru.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/19/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Thiru,

If you are using xml format given by me there is no date node , but there are day, month and year node.
you can change the date label to
<asp:Label ID="lblEdtp" runat="server" Text='<%# Eval("day")+"-"+Eval("month")+"-"+Eval("year") %>'></asp:Label>




Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Ndebata,
Thanks for your reply.
As you know - i am facing and standing in this problem nearly 3days... :-(

Finally i planned to create xml data structure in database and export that table as xml file.
(i just planned - but not yet started to proceed)

Can you use the attachment file (contains: xml file and gridview code i used)

i request you to bind that xml data in gridview -- ?
(pls. refer attachment)
(note: i like to use template field in gridview as mentioned in my code)

Expecting your valuable reply in this regard.


Regards,
Thiru.

 Download source file

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Thiru,

I have created a sample project attached in both C# and VB.net
Please take a look at it.

I have used your xml file to read those data.
and modified
your date label to
<asp:Label ID="lblEdtp" runat="server" Text='<%# Eval("day")+"-"+Eval("month")+"-"+Eval("year") %>'></asp:Label>


Thanks,
Debata



 Download source file

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down

Hope the file is not attached properly.
I cant able download Your attachment - Please check and let me know.

i am getting this error when i try to download your attachment:
404 - File or directory not found.

The resource you are looking for might have been removed, had its name changed, or is temporarily unavailable.


expecting your valuable reply.
Regards,
Thiru

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Getting same error :-(
please send the attachment to: thiru.in@facebook.com

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ndebata on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Thiru could you please try it again,
I have modified the file name, now i can download it from the one posted by me at 10/20/2011 2:44:22 AM

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Hi Debata,
yes i can now - thanks.
i will take a look and get back to you

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Thiru on: 10/20/2011 [Member] Starter | Points: 25

Up
0
Down
Thanks Debata,
Its great its working fine.

Mistake i made is: i just call the xml file and try to databind
hope its suitable for normal xml file.

but in this case we have more than one event on dates
so, it requires to use for loop on each element:
Public Shared Function CollectionBuilder(ByVal xe As XElement) As List(Of NXMLBinder)

Dim lst As New List(Of NXMLBinder)()
For Each ditem As XElement In xe.Elements()
'one date can have more than one events
For Each eitem As XElement In ditem.Elements()
Dim nxobj As New NXMLBinder()

nxobj.day = If(ditem.Attribute("day") IsNot Nothing, ditem.Attribute("day").Value, String.Empty)
nxobj.month = If(ditem.Attribute("month") IsNot Nothing, ditem.Attribute("month").Value, String.Empty)
nxobj.year = If(ditem.Attribute("year") IsNot Nothing, ditem.Attribute("year").Value, String.Empty)

nxobj.title = If(eitem.Element("title") IsNot Nothing, eitem.Element("title").Value, String.Empty)
nxobj.[when] = If(eitem.Element("when") IsNot Nothing, eitem.Element("when").Value, String.Empty)
nxobj.where = If(eitem.Element("where") IsNot Nothing, eitem.Element("where").Value, String.Empty)
nxobj.description = If(eitem.Element("description") IsNot Nothing, eitem.Element("description").Value, String.Empty)

lst.Add(nxobj)
Next
Next
Return lst
End Function


Great code and help Debata - Thanks for timely help.
Regards,
Thiru.

Thiru, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response