将 XML 导入数据集 C# [英] Import XML into Dataset C#
问题描述
I am trying to get a xml file into a dataset and am using the following code:
DataSet ds = new DataSet("TestDataSet");
ds.ReadXml(FileName);
and this xml file:
<Catalog>
<Rec>
<ITEM dt:dt="string"/>
<QTY dt:dt="string">1</QTY>
<SUB dt:dt="string">1</SUB>
<CATALOG dt:dt="string">ABC123</CATALOG>
</Rec>
<Rec>
<ITEM dt:dt="string"/>
<QTY dt:dt="string">1</QTY>
<SUB dt:dt="string">1</SUB>
<CATALOG dt:dt="string">ABC124</CATALOG>
</Rec>
<Rec>
<ITEM dt:dt="string"/>
<QTY dt:dt="string">1</QTY>
<SUB dt:dt="string">1</SUB>
<CATALOG dt:dt="string">ABC125</CATALOG>
</Rec>
</Catalog>
The trouble is that the after setting a watch on ds, it only appears to contain a table called Rec and a column called Rec_Id. If I remove the "dt:dt="String"" datatype everything works fine.
I am using C#.net 2008...
Can someone please advise of the correct way to import this data without having to alter the xml file?
Thanks
As soon as you define your XML namespace used in the XML elements, you can easily import this - no problem.
You need to have your XML look something like this:
<Catalog xmlns:dt="some-xml-namespace-here">
<Rec>
<ITEM dt:dt="string"/>
<QTY dt:dt="string">1</QTY>
<SUB dt:dt="string">1</SUB>
<CATALOG dt:dt="string">ABC123</CATALOG>
</Rec>
.....
</Catalog>
After I do this, your two lines of code work like a charm and the data gets imported, no problem (into 5 tables inside the DataSet).
Marc
这篇关于将 XML 导入数据集 C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!