我尝试使用我的XSD将数据从数据表导出到XML文件。由于我有另一个导入数据的函数,我希望导出看起来相同,所以我从Import-XML-File生成了一个XSD。
导出数据的功能是:
public void Export(string CmdString)
{
cmd = new SqlCommand(CmdString, SqlConnector.conn);
dt = new DataTable("tbl_Adresse");
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dt.WriteXml("..\\..\\..\\XmlDataHandler\\XMLFile_Export.xml", XmlWriteMode.WriteSchema);
}
Import-XML-File如下所示:
<?xml version="1.0" encoding="ISO-8859-1"?>
<tbl_address>
<row delete="true" />
<row Forename="Simon" Surname="Baker" MobileNr="+193434278" />
<row Forename="Johnny" Surname="Depp" MobileNr="+173953278" />
<row Forename="Michael" Surname="Jackson" MobileNr="+127425978" />
<row Forename="Elvis" Surname="Prasley" MobileNr="+163645378" />
<row Forename="Axl" Surname="Rose" MobileNr="+173975618" />
</tbl_address>
Schema看起来像这样:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="tbl_address">
<xs:complexType>
<xs:sequence>
<xs:element name="row" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="delete" type="xs:string"></xs:attribute>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
目前,Export-XML-File看起来像这样:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="tbl_address" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="tbl_address">
<xs:complexType>
<xs:sequence>
<xs:element name="Forename" type="xs:string" minOccurs="0" />
<xs:element name="Surname" type="xs:string" minOccurs="0" />
<xs:element name="MobileNr" type="xs:string" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<tbl_address>
<Forename>Simon</Forename>
<Surname>Baker</Surname>
<MobileNr>+193434278</MobileNr>
</tbl_address
<tbl_address>
<Forename>Johny</Forename>
<Surname>Depp</Surname>
<MobileNr>+173953278</MobileNr>
</tbl_address
<tbl_address>
<Forename>Michael</Forename>
<Surname>Jackson</Surname>
<MobileNr>+127425978</MobileNr>
</tbl_address
<tbl_address>
<Forename>Elvis</Forename>
<Surname>Prasley</Surname>
<MobileNr>+163645378</MobileNr>
</tbl_address
<tbl_address>
<Forename>Axl</Forename>
<Surname>Rose</Surname>
<MobileNr>+173975618</MobileNr>
</tbl_address
</NewDataSet>
我需要更改导出文件与导入文件相同的内容吗?
答案 0 :(得分:0)
除了向您的DataTable对象添加架构(如其他答案所述),我相信您的架构本身需要调整。
我使用XSD来解释您的输入XML(作为示例)并将其转换为XSD架构表示。下面的架构是XSD返回的:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="tbl_address" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="tbl_address" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="row">
<xs:complexType>
<xs:attribute name="delete" type="xs:string" />
<xs:attribute name="Forename" type="xs:string" />
<xs:attribute name="Surname" type="xs:string" />
<xs:attribute name="MobileNr" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
请注意,它与发布的架构略有不同。请按照建议尝试两个调整(将模式添加到DataTable对象PLUS,修改模式以更紧密地匹配数据)并报告回来!
答案 1 :(得分:0)
我现在解决了。
Export-Function看起来像这样:
public void Export(string CmdString)
{
cmd = new SqlCommand(CmdString, SqlConnector.conn);
da = new SqlDataAdapter(cmd);
dt = new DataTable("row");
ds = new DataSet("tbl_adress");
dt.ReadXmlSchema("..\\..\\..\\XmlDataHandler\\XmlSchema.xsd");
SqlConnector.da.Fill(dt);
ds.Tables.Add(dt);
ds.WriteXml("..\\..\\..\\XmlDataHandler\\XMLFile_Export.xml");
}
和Schema是这样的:
<?xml version="1.0" encoding="iso-8859-1"?>
<xs:schema id="tbl_adress" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="tbl_adress" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="row">
<xs:complexType>
<xs:attribute name="delete" type="xs:string" />
<xs:attribute name="Forename" type="xs:string" />
<xs:attribute name="Surname" type="xs:string" />
<xs:attribute name="MobileNr" type="xs:string" />
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
我创建了一个名为&#34; tbl_adress&#34;的数据集。并将DataTable放在此DataSet中。 通过这个,根节点元素被称为&#34; tbl_adress&#34;。
使用其他答案我得到了解决问题的方法。