xsd 通用树结构,使用 xsd.exe 编译为对象模型

发布于 2024-08-08 05:30:05 字数 119 浏览 1 评论 0原文

假设我希望我的 xml 包含任意数量的容器标签,其中每一个标签又包含任意数量的容器标签,依此类推。 xsd 会是什么样子?

PS

我希望这个 xsd 被编译成类。

非常感谢。

say i want my xml to include any number of CONTAINER tags, which every one of those to include yet again any number of container tags, and so on. how would the xsd look like?

p.s.

i want this xsd to be compiled to classes.

thank you very much.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心凉怎暖 2024-08-15 05:30:05

XSD 可能如下所示:

<xs:schema
   elementFormDefault    ="qualified"
   targetNamespace       ="urn:Cheeso._2009oct.ContainerExample.Data"
   xmlns:tns             ="urn:Cheeso._2009oct.ContainerExample.Data"
   xmlns:xs              ="http://www.w3.org/2001/XMLSchema"
   >

  <!-- a complex type or structure -->
  <xs:complexType name="MyComplexType">
      <xs:sequence>
        <xs:element name="CONTAINER" maxOccurs="unbounded" nillable="true" type="tns:MyComplexType" />
      </xs:sequence>
      <xs:attribute name="Id" type="xs:string"/>
  </xs:complexType>

  <xs:element name="CONTAINER" nillable="true" type="tns:MyComplexType" />

</xs:schema>

符合此架构的 XML 可能如下所示:

<CONTAINER Id="L001.N001" xmlns="urn:Cheeso._2009oct.ContainerExample.Data">
  <CONTAINER Id="L002.N001" />
  <CONTAINER Id="L002.N002" />
  <CONTAINER Id="L002.N003">
    <CONTAINER Id="L003.N001">
      <CONTAINER Id="L004.N001" />
      <CONTAINER Id="L004.N002" />
      <CONTAINER Id="L004.N003" />
    </CONTAINER>
    <CONTAINER Id="L003.N002">
      <CONTAINER Id="L004.N004">
        <CONTAINER Id="L005.N001" />
        <CONTAINER Id="L005.N002" />
      </CONTAINER>
      <CONTAINER Id="L004.N005">
        <CONTAINER Id="L005.N003" />
        <CONTAINER Id="L005.N004" />
        <CONTAINER Id="L005.N005" />
      </CONTAINER>
      <CONTAINER Id="L004.N006" />
    </CONTAINER>
  </CONTAINER>
</CONTAINER>

它可以嵌套到任意深度。

生成如下类: xsd.exe /c Foo.xsd 。这些类看起来像这样:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Cheeso._2009oct.ContainerExample.Data")]
[System.Xml.Serialization.XmlRootAttribute("CONTAINER", Namespace="urn:Cheeso._2009oct.ContainerExample.Data", IsNullable=true)]
public partial class MyComplexType {

    private MyComplexType[] cONTAINERField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("CONTAINER", IsNullable=true)]
    public MyComplexType[] CONTAINER {
        get {
            return this.cONTAINERField;
        }
        set {
            this.cONTAINERField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}

The XSD might look like this:

<xs:schema
   elementFormDefault    ="qualified"
   targetNamespace       ="urn:Cheeso._2009oct.ContainerExample.Data"
   xmlns:tns             ="urn:Cheeso._2009oct.ContainerExample.Data"
   xmlns:xs              ="http://www.w3.org/2001/XMLSchema"
   >

  <!-- a complex type or structure -->
  <xs:complexType name="MyComplexType">
      <xs:sequence>
        <xs:element name="CONTAINER" maxOccurs="unbounded" nillable="true" type="tns:MyComplexType" />
      </xs:sequence>
      <xs:attribute name="Id" type="xs:string"/>
  </xs:complexType>

  <xs:element name="CONTAINER" nillable="true" type="tns:MyComplexType" />

</xs:schema>

The XML that conforms to this schema might look like this:

<CONTAINER Id="L001.N001" xmlns="urn:Cheeso._2009oct.ContainerExample.Data">
  <CONTAINER Id="L002.N001" />
  <CONTAINER Id="L002.N002" />
  <CONTAINER Id="L002.N003">
    <CONTAINER Id="L003.N001">
      <CONTAINER Id="L004.N001" />
      <CONTAINER Id="L004.N002" />
      <CONTAINER Id="L004.N003" />
    </CONTAINER>
    <CONTAINER Id="L003.N002">
      <CONTAINER Id="L004.N004">
        <CONTAINER Id="L005.N001" />
        <CONTAINER Id="L005.N002" />
      </CONTAINER>
      <CONTAINER Id="L004.N005">
        <CONTAINER Id="L005.N003" />
        <CONTAINER Id="L005.N004" />
        <CONTAINER Id="L005.N005" />
      </CONTAINER>
      <CONTAINER Id="L004.N006" />
    </CONTAINER>
  </CONTAINER>
</CONTAINER>

It can nest to arbitrary depth.

Generate the classes like this: xsd.exe /c Foo.xsd . The classes look like this:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.42")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="urn:Cheeso._2009oct.ContainerExample.Data")]
[System.Xml.Serialization.XmlRootAttribute("CONTAINER", Namespace="urn:Cheeso._2009oct.ContainerExample.Data", IsNullable=true)]
public partial class MyComplexType {

    private MyComplexType[] cONTAINERField;

    private string idField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("CONTAINER", IsNullable=true)]
    public MyComplexType[] CONTAINER {
        get {
            return this.cONTAINERField;
        }
        set {
            this.cONTAINERField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id {
        get {
            return this.idField;
        }
        set {
            this.idField = value;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文