C#:DataSet.readXML(“filepath”)如何与对象内对象中的对象的XML文件配合使用? [英] C#: How does DataSet.readXML( "filepath" ) work with my XML file that has objects within objects within objects?

查看:152
本文介绍了C#:DataSet.readXML(“filepath”)如何与对象内对象中的对象的XML文件配合使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个格式如下的xml文件:

 < items> 
< item>
< itemProperty1> Propeterty1< / itemProperty1>
< itemProperty2> Propeterty2< / itemProperty2>
< propertyWithSubProperties1>
< subprop1> subProp1< / subProp1>
< subprop2> subProp2< / subProp2>
< depthPropertyWithSubProperties1>
< depthSubProperty1> data< / depthSubProperty1>
< depthSubProperty2> data2< / depthSubProperty2>
< / depthPropertyWithSubProperties1>
< / propertyWithSubProperties1>
< / item>
...更多项目
< / items>

我试图使用c#中的数据集读取这个数据文件,如下所示:

  DataSet dataSet = new DataSet(); 
dataSet.ReadXml(fileName);

我可以访问前两个项目没有这样的问题:

  firstProperty = dataSet.Tables [0] .Rows [i] [0] .ToString(); 
secondProperty = dataSet.Tables [0] .Rows [i] [1] .ToString();

但是,我完全不知道如何获取其他项目中的信息,因为他们是一级或更深。我通常的谷歌搜索没有任何用途。

解决方案

您的 DataSet 将包含三个表:




  • dataSet.Tables [0]

    itemProperty1 itemProperty2 item_Id


  • dataSet.Tables [1]

    subProp1 subProp2 propertyWithSubProperties1_Id item_Id / p>


  • dataSet.Tables [2]

    deepSubProperty1 deepSubProperty2 propertyWithSubProperties1_Id




表[0] 链接到表[1] 使用自动生成的 item_Id 列。



表[1] 使用自动生成的 propertyWithSubProperties1_Id 列链接到 Tables [2] p>

I've got an xml file formatted like this:

<items>
<item>
    <itemProperty1>Propeterty1</itemProperty1>
    <itemProperty2>Propeterty2</itemProperty2>
    <propertyWithSubProperties1>
       <subprop1>subProp1</subProp1>
       <subprop2>subProp2</subProp2>
       <deeperPropertyWithSubProperties1>
            <deeperSubProperty1>data</deeperSubProperty1>
            <deeperSubProperty2>data2</deeperSubProperty2>
       </deeperPropertyWithSubProperties1>
    </propertyWithSubProperties1>
</item>
...More items
</items>

I'm trying to use a dataset in c# to read in this datafile like this:

DataSet dataSet = new DataSet();
dataSet.ReadXml(fileName);

I can access the first two items no problem like this:

firstProperty = dataSet.Tables[0].Rows[i][0].ToString();
secondProperty = dataSet.Tables[0].Rows[i][1].ToString();

But nothing I'm completely baffled as to how to get at the information in the other items because they are a level or more deeper. My usual google searches turn up nothing of use.

解决方案

Your DataSet will contain three tables:

  • dataSet.Tables[0]:
    Columns itemProperty1, itemProperty2 and item_Id.

  • dataSet.Tables[1]:
    Columns subProp1, subProp2, propertyWithSubProperties1_Id and item_Id.

  • dataSet.Tables[2]:
    Columns deeperSubProperty1, deepSubProperty2 and propertyWithSubProperties1_Id .

Tables[0] is linked to Tables[1] using the auto-generated item_Id columns.

Tables[1] is linked to Tables[2] using the auto-generated propertyWithSubProperties1_Id columns.

这篇关于C#:DataSet.readXML(“filepath”)如何与对象内对象中的对象的XML文件配合使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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