I have following simple XSD file:
<xs:element name="Search" type="SearchObject"/>
<xs:complexType name="SearchObject">
<xs:choice>
<xs:element name="Simple" type="SimpleSearch"/>
<xs:element name="Extended" type="ExtendedSearch"/>
</xs:choice>
</xs:complexType>
<xs:complexType name="SimpleSearch">
<xs:sequence>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="ExtendedSearch">
<xs:sequence>
<xs:element name="FirstName" type="xs:string"/>
<xs:element name="LastName" type="xs:string"/>
<xs:element name="Age" type="xs:int"/>
<xs:element name="Address" type="xs:string"/>
</xs:sequence>
</xs:complexType>
I use Visual Studio Shell like this:
xsd XMLSchema.xsd /c
Basically /c stands for generating C# classes out of XMLSchema.xsd.
The classes then look something like this one:
[System.Xml.Serialization.XmlRootAttribute("Search", Namespace="http://tempuri.org/XMLSchema.xsd", IsNullable=false)]
public partial class SearchObject {
private object itemField;
[System.Xml.Serialization.XmlElementAttribute("Extended", typeof(ExtendedSearch))]
[System.Xml.Serialization.XmlElementAttribute("Simple", typeof(SimpleSearch))]
public object Item {
get {
return this.itemField;
}
set {
this.itemField = value;
}
}
}
My first question is why is the property "Item" not called "Search" as I have set inside xsd file on that element?
My second question is why is property Item of type object? I have set a choice inside my xsd file and I would like the c# code to look more like this:
public partial class SearchObject<T> where T : SimpleSearch, where T : ExtendedSearch
{
public T Search
{
get ...
set ...
}
}
I would like to have somehow an generic class that allows only the types which I have specified inside the choice block in xsd file which are in my case SimpleSearch and ExtendedSearch.
Is that even possible and if yes how do I get it right?