How do I load the following nested XML into a DataSet?
<items>
<item>
<id>i1</id>
<name>item1</name>
<subitems>
<name>subitem1</name>
<name>subitem2</name>
</subitems>
</item>
<item>
<id>i2</id>
<name>item2</name>
<subitems>
<name>subitem1</name>
<name>subitem2</name>
</subitems>
</item>
</items>
I can get as far as a the "item" table but how do I get the subitems?
MemoryStream itemsStream = new MemoryStream(Encoding.ASCII.GetBytes(itemsXML));
DataSet itemsSet = new DataSet();
itemsSet.ReadXml(itemsStream);
foreach (DataRow itemRow in itemsSet.Tables["item"].Rows) {
Console.WriteLine("column id=" + itemRow["id"] as string + " name=" + itemRow["name"] as string);
}
Use dataset ReadXml. You have to follow article on link below backwards:
http://msdn.microsoft.com/en-us/library/7sfkwf9s(v=VS.100).aspx
For Instance:
Of course you can read tables individually by using
ds.Tables[0]
andds.Tables[1]
This works for me, the only liberty I have taken is to Change the field name for the subitems.
Original XML for subitem
Modified XML for subitem
Here is the code.