【问题标题】:Using C# Dataset to read xml file使用 C# Dataset 读取 xml 文件
【发布时间】:2015-02-19 15:07:22
【问题描述】:

我有一个包含以下数据的 xml 文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Tables xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <countries>
        <country country_id = "1" name = "Afghanistan"/>
        <country country_id = "2" name = "Albania"/>
        <country country_id = "3" name = "Algeria"/>
    </countries>
</Tables>

当我尝试使用下面的 C# 代码时,

DataSet ds_XMLData= new DataSet();
ds_XMLData.ReadXml(XMLFile);

我有两张桌子: 具有一列countris_id 和一行值为“0”的国家/地区 具有 3 列的国家 - 附加列是国家 ID,所有行的值为“0”

您能否帮我理解为什么数据集中没有一个表国家,只有两列 - country_id 和 name?

另外,为什么在所有表格中都添加了额外的列 - countries_id。

注意:- 我想继续使用基于属性的 xml 文件格式。

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    很简单,你可以用XmlReader.ReadToFollowing Method 来做:

    xmlFile.ReadToFollowing("countries");
    DataSet ds_XMLData= new DataSet();
    ds_XMLData.ReadXml(XMLFile);
    

    【讨论】:

      【解决方案2】:

      只需去掉 &lt;countries /&gt; 元素。例如,

      <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
      <Tables xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
         <country country_id = "1" name = "Afghanistan"/>
         <country country_id = "2" name = "Albania"/>
         <country country_id = "3" name = "Algeria"/>
      </Tables>
      

      我不是 DataSet 专家,但我的猜测是它试图创建从 countriescountry 的关系,而你不想要。如果可以控制 XML,只需修改 XML。

      【讨论】:

      • 非常感谢 Didaxis。删除国家标签就可以了。谢谢大家的投入。我现在对 XML 有了更好的理解。
      【解决方案3】:

      我不知道为什么DataSet 创建两个表和附加列。但我认为您必须在从 XML 加载数据之前阅读 XML 架构。

      DataSet dataSet = new DataSet();
      dataSet.ReadXmlSchema("countries.xsd");
      dataSet.ReadXml("countries.xml");
      

      countries.xsd:

      <?xml version="1.0" standalone="yes"?>
      <xs:schema id="Tables" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
        <xs:element name="countries" msdata:IsDataSet="true" msdata:MainDataTable="countries" msdata:UseCurrentLocale="true">
          <xs:complexType>
            <xs:choice minOccurs="0" maxOccurs="unbounded">
              <xs:element name="country">
                <xs:complexType>
                  <xs:attribute name="country_id" type="xs:int" msdata:AutoIncrement="true" msdata:AutoIncrementSeed="1"/>
                  <xs:attribute name="name" type="xs:string" />
                </xs:complexType>
              </xs:element>
            </xs:choice>
          </xs:complexType>
        </xs:element>
      </xs:schema>
      

      加载架构后,DataSet 将具有名称 countries 和一个名为 country 的表。

      【讨论】:

        【解决方案4】:
        • 有两个表,因为您的 xml 文件中有两个复杂的 xml 元素:&lt;countries&gt;&lt;country&gt;

        • 添加了附加列“countries_id”(元素的父 ID)第二个表,以指定该元素位于 &lt;countries&gt; 内,父 ID 为“countries_id”=0。您可以在 XML 中再添加一个&lt;countries&gt;,并检查第二个表值中的“countries_id”父 ID“0”和“1”。

        `

        <?xml version="1.0" encoding="utf-8" standalone="yes"?>
        <Tables xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
          <countries>
            <country country_id = "1" name = "Afghanistan"/>
            <country country_id = "2" name = "Albania"/>
            <country country_id = "3" name = "Algeria"/>
          </countries>
          <countries>
            <country country_id = "1" name = "Afghanistan"/>
            <country country_id = "2" name = "Albania"/>
            <country country_id = "3" name = "Algeria"/>
          </countries>
        </Tables>
        

        `

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2013-01-02
          • 2010-09-08
          • 1970-01-01
          • 1970-01-01
          • 2015-12-23
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多