我有以下架构:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="WSParam">
<xs:complexType>
<xs:sequence>
<xs:element name="domain" type="xs:string"/>
<xs:element name="userName" type="xs:string"/>
<xs:element name="Cases">
<xs:complexType>
<xs:sequence>
<xs:element name="caseName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我期望 DataSet.GetXml() 的输出如下:
<NewDataSet>
<WSParam>
<domain>ad</domain>
<userName>admin</userName>
<Cases>
<caseName>case 1</caseName>
</Cases>
</WSParam>
</NewDataSet>
但输出是:
<NewDataSet>
<WSParam>
<domain>ad</domain>
<userName>admin</userName>
</WSParam>
<Cases>
<caseName>case 1</caseName>
</Cases>
</NewDataSet>
任何人都向我确认具有复杂类型的 DataSet.GetXml() 的正确行为,否则我会错过一些东西。
非常感谢任何指导。
更新1:
我正在使用 DataSet.ReadXmlSchema("...xsd")。由此,我使用 DataSet.Tables[..] 创建 DataTable 来放置数据。然后调用 DataSet.GetXml() 来获取字符串 XML 表示。
更新2:
代码:
private void button2_Click(object sender, EventArgs e)
{
String sXSD = @"C:\WindowsFormsApplication1\WindowsFormsApplication1\XMLFile1.xsd";
DataSet dsXSD = new DataSet();
dsXSD.ReadXmlSchema(sXSD);
int k = 0;
do
{
DataRow d;
d = dsXSD.Tables["tblUser"].NewRow();
int i = 0;
do
{
d[i] = k;
i = (i + 1);
} while (i < dsXSD.Tables["tblUser"].Columns.Count);
dsXSD.Tables["tblUser"].Rows.Add(d);
d = dsXSD.Tables["tblUserRole"].NewRow();
i = 0;
do
{
d[i] = k;
i = (i + 1);
} while (i < dsXSD.Tables["tblUserRole"].Columns.Count);
dsXSD.Tables["tblUserRole"].Rows.Add(d);
k = (k + 1);
} while (k < 3);
dsXSD.WriteXml("c:\\test.xml");
}
XSD:
<?xml version="1.0" encoding="utf-16" ?>
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" version="1.0"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:element name="tblUser">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="UserID" type="xsd:int" />
<xsd:element name="FullName" type="xsd:string" nillable="true" />
<xsd:element name="tblUserRole">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="UserID" type="xsd:int" />
<xsd:element name="RoleID" type="xsd:int" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
输出:
<?xml version="1.0" standalone="yes"?>
<NewDataSet>
<tblUser>
<UserID>0</UserID>
<FullName>0</FullName>
<tblUserRole>
<UserID>0</UserID>
<RoleID>0</RoleID>
</tblUserRole>
</tblUser>
<tblUser>
<UserID>1</UserID>
<FullName>1</FullName>
<tblUserRole>
<UserID>1</UserID>
<RoleID>1</RoleID>
</tblUserRole>
</tblUser>
<tblUser>
<UserID>2</UserID>
<FullName>2</FullName>
<tblUserRole>
<UserID>2</UserID>
<RoleID>2</RoleID>
</tblUserRole>
</tblUser>
</NewDataSet>