我怎么能读取使用ReadXML的XML属性?如何做dataset.readxml转化为表? [英] how can I read an xml attribute using readXML? how does dataset.readxml translate into tables?

查看:206
本文介绍了我怎么能读取使用ReadXML的XML属性?如何做dataset.readxml转化为表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想知道如何从ReadXML的结果表的样子,说,如果XML文件是这样的:

 <项目ATTR =一些属性>
< descirption>&什么LT; /描述>
< /项目>

我可以Tables集合像这样直接引用表

  ds.ReadXml(XML);
... ds.Tables [I]

然后我可以访问使用行集合的行和列:

  ds.Tables [3] .Rows [I] .ItemArray(J);

但我怎么可以访问任何XML节点的属性?


解决方案

这是在该行。我定你的XML一点:)

  VAR XML =<项目ATTR = \\一些属性\\><描述>什么< /描述>< /项目>中;
变种DS =新的DataSet();ds.ReadXml(新StringReader(XML),XmlReadMode.Auto);变种IA = ds.Tables [0] .Rows [0] .ItemArray;
变种ATT = IA [1]; // ATT ==一些属性

如果你没有一个模式,你可能要检查列,以确定它是什么。

每注释:您将看到我让它推断架构(XmlReadMode.Auto)。这需要将根节点作为行然后添加在顺序的属性,然后在元件的值下的元素。因此,例如,下面的XML ...

  VAR XML =<项目>
             < ATTR1 ='ATTR1'attR2位='attR2位'&GT项目;
                 <描述>&DESC1 LT; /描述>
             < /项目>
             < ATTR1 ='attr3'attR2位='attr4'&GT项目;
                 <描述>&DESC2 LT; /描述>< /项目>
           < /项目>中;

我将得到两行(每个项目),与栏目的ATTR1,attR2位和说明。你可以改变使用模式这间$ P $点的XML的方式。

I just want to know how does the table resulting from readXML look like, say if the xml file looks like this:

<item attr="some attribute">
<descirption>anything</description>
</item>

I can reference tables directly by the Tables collection like this:

ds.ReadXml(xml);
... ds.Tables[i]

then I can access rows and columns using the rows collection:

ds.Tables[3].Rows[i].ItemArray(j);

but how can I access an "attribute" of any xml node?

解决方案

It's in the Row. I fixed your xml a bit :)

var xml = "<item attr=\"some attribute\"><description>anything</description></item>";
var ds = new DataSet();

ds.ReadXml( new StringReader( xml ), XmlReadMode.Auto );

var ia = ds.Tables[0].Rows[0].ItemArray;
var att = ia[1]; // att == "some attribute"

If you don't have a schema, you might have to check the column to determine what it is.

Per comment: You will see I am letting it infer the schema (XmlReadMode.Auto). It takes elements under the root node as Rows then adds the attributes in order and then the value in the element. So for example the following XML ...

var xml = "<items>
             <item attr1='attr1' attr2='attr2'>
                 <description>desc1</description>
             </item>
             <item attr1='attr3' attr2='attr4'>
                 <description>desc2</description></item>
           </items>";

I will get two rows (one for each item) with Columns for attr1, attr2 and description. You can change the way it interprets the XML using a schema.

这篇关于我怎么能读取使用ReadXML的XML属性?如何做dataset.readxml转化为表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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