Load nested XML into dataset

2019-04-16 10:17发布

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);
 }

标签: c# xml dataset
3条回答
狗以群分
2楼-- · 2019-04-16 10:55
String sss = @"<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>";

StringReader rr = new StringReader(sss);
DataSet sdread = new DataSet();
sdread.ReadXml(rr);
查看更多
放荡不羁爱自由
3楼-- · 2019-04-16 10:59

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:

DataSet ds = new DataSet();
ds.ReadXml(new StringReader(nestedXml));
StringBuilder sb = new StringBuilder();
ds.WriteXml(new StringWriter(sb));
Response.Write(sb.ToString());

Of course you can read tables individually by using ds.Tables[0] and ds.Tables[1]

查看更多
唯我独甜
4楼-- · 2019-04-16 11:03

This works for me, the only liberty I have taken is to Change the field name for the subitems.

Original XML for subitem

   <subitems>
     <name>subitem1</name>
     <name>subitem2</name>
   </subitems>

Modified XML for subitem

   <subitems>
       <name1>subitem1</name1>
       <name2>subitem2</name2>
   </subitems>

Here is the code.

        DataSet myDS = new DataSet();
        DataTable myTable = new DataTable("item");
        myTable.Columns.Add("id", typeof(string));
        myTable.Columns.Add("name", typeof(string));
        myTable.Columns.Add("name1", typeof(string));
        myTable.Columns.Add("name2", typeof(string));

        myDS.Tables.Add(myTable);

        string xmlData = "<items>  <item>   <id>i1</id>   <name>item1</name>   <subitems>    <name1>subitem1</name1>    <name2>subitem2</name2>   </subitems>  </item>  <item>   <id>i2</id>   <name>item2</name>   <subitems>    <name1>subitem3</name1>    <name2>subitem4</name2>   </subitems></item></items>";
        System.IO.MemoryStream itemsStream = new System.IO.MemoryStream(Encoding.ASCII.GetBytes(xmlData));

        myDS.ReadXml(itemsStream, XmlReadMode.IgnoreSchema);
        foreach (DataRow itemRow in myDS.Tables["item"].Rows)
        {
            MessageBox.Show("column id=" + itemRow["id"]  + " name=" + itemRow["name"]);
            MessageBox.Show(itemRow["name1"].ToString() + " - " + itemRow["name2"].ToString());
        }
查看更多
登录 后发表回答