C# 将嵌套的 XML 加载到数据集中
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/759243/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Load nested XML into dataset
提问by sipwiz
How do I load the following nested XML into a DataSet?
如何将以下嵌套 XML 加载到 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);
}
采纳答案by Anand Shah
This works for me, the only liberty I have taken is to Change the field name for the subitems.
这对我有用,我唯一的自由是更改子项的字段名称。
Original XML for subitem
子项的原始 XML
<subitems>
<name>subitem1</name>
<name>subitem2</name>
</subitems>
Modified XML for subitem
子项的修改 XML
<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());
}
回答by Pablo
Use dataset ReadXml. You have to follow article on link below backwards:
使用数据集 ReadXml。您必须反向关注下面链接上的文章:
http://msdn.microsoft.com/en-us/library/7sfkwf9s(v=VS.100).aspx
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]
当然,您可以使用ds.Tables[0]
和单独读取表格ds.Tables[1]
回答by Chander Parkash
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);