将XML导入数据集C# [英] Import XML into Dataset C#

查看:145
本文介绍了将XML导入数据集C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个xml文件转换为数据集,并使用以下代码:

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

和此xml文件:

<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>

麻烦的是,在ds上设置手表后,它只显示包含一个名为Rec和一个名为Rec_Id的列。如果我删除dt:dt =String数据类型一切正常。

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.

我正在使用C#.net 2008 ...

I am using C#.net 2008...

有人可以建议您正确导入此数据,而无需更改xml文件?

Can someone please advise of the correct way to import this data without having to alter the xml file?

谢谢

推荐答案

只要定义XML元素中使用的XML命名空间,就可以轻松导入 - 没有问题。

As soon as you define your XML namespace used in the XML elements, you can easily import this - no problem.

您需要将您的XML看起来像这样:

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>

执行此操作后,您的两行代码的工作方式就像一个魅力,数据被导入,没有问题(在DataSet中的5个表格中)。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆