[英]Using C# Dataset to read xml file
I have an xml file with the below data: 我有一个包含以下数据的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>
When I try to use the below C# code, 当我尝试使用以下C#代码时,
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" 我得到两个表:具有一栏countris_id的国家和具有值“ 0”的一行具有三列的国家-另一列为country_id和所有值为“ 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? 能否请您帮我了解为什么在数据集中只有两列(country_id和name)没有显示单个表国家/地区?
Also, why is the additional column - countries_id added in all the tables. 此外,为什么还要在所有表中添加附加列-country_id。
Note:- I would like to keep on using the attribute based xml file format. 注意:-我想继续使用基于属性的xml文件格式。
Simply, You can do it with XmlReader.ReadToFollowing Method : 只需使用XmlReader.ReadToFollowing方法即可 :
xmlFile.ReadToFollowing("countries");
DataSet ds_XMLData= new DataSet();
ds_XMLData.ReadXml(XMLFile);
Just get rid of the <countries />
element. 只是摆脱<countries />
元素。 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. 我不是DataSet
专家,但我想它正在尝试建立您不想要的countries
country
与countries
country
之间的关系。 If you can control the XML, just modify the XML. 如果可以控制XML,则只需修改XML。
I don't know why DataSet
creates two tables and additional columns. 我不知道为什么DataSet
创建两个表和其他列。 But I think you must read XML schema before load data from XML. 但是我认为您必须先阅读XML模式,然后才能从XML加载数据。
DataSet dataSet = new DataSet();
dataSet.ReadXmlSchema("countries.xsd");
dataSet.ReadXml("countries.xml");
countries.xsd
: 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
. 装载模式后, DataSet
将有名字countries
与一个名为表country
。
There are two tables because there are two complex xml elements in your the xml file : <countries>
and <country>
有两个表,因为xml文件中有两个复杂的xml元素: <countries>
和<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. 添加了第二列“ countries_id”(元素的父ID)第二列,以指定元素位于父ID为“ countries_id” = 0的<countries>
内部。 You can add one more <countries>
in XML and check the "countries_id" parent ID "0" and "1" in 2nd table values. 您可以在XML中再添加一个<countries>
,并在第二个表值中检查“ 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>
` `
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.