views:

129

answers:

1

hi, 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.

+2  A: 

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;
        }
    }
}
Cheeso
thanks i'll check it out
Itay