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;
}
}
}