Alverant
Alverant

Reputation: 229

LoadXml isn't loading text properly

I'm trying to get the hang of using XML in C# and VB.NET. I hardcoded some XML into a XmlDocument variable then took a count of the child nodes then bound the XML variable to a gridview. There should be two child nodes and the gridview should have two rows with three columns (Title, Description, Date).

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml("<tasks><Task><Title>a</Title><Description>b</Description><Date>c</Date></Task><Task><Title>d</Title><Description>e</Description><Date>f</Date></Task></tasks>");
litTest.Text = xmlDoc.ChildNodes.Count.ToString();
gvData.DataSource = xmlDoc;
gvData.DataBind();

Instead I get one row with name, localname, namespaceURL, InnerXML, InnerText, etc. I'm not sure what I'm doing wrong.

Upvotes: 0

Views: 118

Answers (1)

James Dev
James Dev

Reputation: 3009

You can load the xml into a Dataset and then bind that Dataset to the Gridview, might be the easiest option

DataSet ds = new DataSet();
String MyXml = "<tasks><Task><Title>a</Title><Description>b</Description><Date>c</Date></Task><Task><Title>d</Title><Description>e</Description><Date>f</Date></Task></tasks>";
StringReader sr = new StringReader(MyXml);
ds.ReadXml(sr);
gvData.DataSource = ds;
gvData.DataBind();

Upvotes: 1

Related Questions