简体   繁体   中英

Using C# Dataset to read xml file

I have an xml file with the below data:

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

When I try to use the below C# code,

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

I get two tables: countries with one column countris_id and one row with value "0" country with 3 columns - additional column being countries_id and all rows with value "0"

Can you please help me understand why is a single table countries not present in the dataset with only two columns - country_id and name?

Also, why is the additional column - countries_id added in all the tables.

Note:- I would like to keep on using the attribute based xml file format.

Simply, You can do it with XmlReader.ReadToFollowing Method :

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

Just get rid of the <countries /> element. Eg,

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

I'm no DataSet expert, but my guess is it's trying to create a relationship from countries to country that you don't want. If you can control the XML, just modify the XML.

I don't know why DataSet creates two tables and additional columns. But I think you must read XML schema before load data from 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>

After loading schema, DataSet will have name countries with one table named country .

  • There are two tables because there are two complex xml elements in your the xml file : <countries> and <country>

  • The additional column "countries_id"(element's Parent ID) second table is added to specify that element lies inside <countries> with parent id "countries_id"=0. You can add one more <countries> in XML and check the "countries_id" parent ID "0" and "1" in 2nd table values.

`

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

`

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM