2

我有以下架构:

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

1 回答 1

0

我不确定是否可以使用数据集。

但是你可以通过创建一个非标准的微软数据集 xsd 来自己做。它需要一些工作,但您可以随后根据需要对其进行调整。

关键是使用 xs:group。

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
  <xs:element name="NewDataSet">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="WSParam">
          <xs:complexType>
            <xs:sequence>
              <xs:element name="domain" type="xs:string" minOccurs="0" />
              <xs:element name="userName" type="xs:string" minOccurs="0" />
              <xs:group ref="CasesGroup" minOccurs="0"/>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

    <xs:group name="CasesGroup">
    <xs:sequence>
      <xs:element name="Cases">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="CaseName" minOccurs="1" maxOccurs="unbounded">
              <xs:complexType>
                <xs:simpleContent>
                  <xs:extension base="xs:string">
                  </xs:extension>
                </xs:simpleContent>
              </xs:complexType>
            </xs:element>
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:sequence>
  </xs:group>
</xs:schema>

对于上述 xsd(我将其命名为 wsparam.xsd)运行“xsd.exe wsparam.xsd /classes /n:Yournamespace”

现在您可以从代码中填充数据

    NewDataSetWSParam p = new NewDataSetWSParam();
    p.domain = "ad";
    p.userName = "admin";
    p.Cases = new NewDataSetWSParamCaseName[] {  
            new NewDataSetWSParamCaseName () { Value ="case 1" }
    };

现在是棘手的部分在序列化以从 XML 文件中剥离命名空间属性时使用非标准 XmlTextWriter

public class NoNamespaceXmlWriter : XmlTextWriter
{
            public NoNamespaceXmlWriter(System.IO.TextWriter output)
                : base(output)
            {
                Formatting = System.Xml.Formatting.None;
            }

            public override void WriteStartDocument() { }
            public override void WriteStartElement(string prefix, string localName, string ns)
            {
                base.WriteStartElement("", localName, "");
            }
        }

使用下面的代码序列化您的对象

string serialXML = string.Empty;
        XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
        ns.Add("", "");

        XmlSerializer serializer = new XmlSerializer(p.GetType());
        StringBuilder sb = new StringBuilder();
        StringWriter sw = new StringWriter(sb);
        using (XmlWriter writer = new RDECommon.Xml.NoNamespaceXmlWriter(sw))
        {
            serializer.Serialize(writer, p, ns);
            serialXML = sb.ToString();
        }

serialXML 包含您的 xml

<NewDataSet>
    <WSParam>
        <domain>ad</domain>
        <userName>admin</userName>
        <Cases>
            <caseName>case 1</caseName>
        </Cases>
    </WSParam>
</NewDataSet>
于 2013-07-04T06:46:47.597 回答