I have a set of XML schema files provided to me. I cannot changed the XML as these will be updated on occasion. I am using xsd.exe to convert the schema files to generated c# code. I cannot use any third party tools. Part of one of the XML schema files appears as such:
<xs:complexType name="LocationType">
<xs:choice minOccurs="1" maxOccurs="1">
<xs:element name="LocNum" minOccurs="1" maxOccurs="1" type="xs:string" />
<xs:sequence>
<xs:element name="Name" minOccurs="0" maxOccurs="1" type="xs:string" />
<xs:element name="Address" minOccurs="0" maxOccurs="1" type="xs:string" />
<xs:element name="City" minOccurs="1" maxOccurs="1" type="xs:string" />
<xs:element name="State" minOccurs="0" maxOccurs="1">
</xs:sequence>
</xs:choice>
</xs:complexType>
When converted to c#, I get a result such as this:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://abcxyz.com")]
public partial class LocationType
{
private object[] itemsField;
private ItemsChoiceType[] itemsElementNameField;
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("Address", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("City", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("LocNum", typeof(string), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("Longitude", typeof(decimal), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlElementAttribute("State", typeof(LocationTypeState), Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
[System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemsElementName")]
public object[] Items
{
get { return this.itemsField; }
set { this.itemsField = value; }
}
/// <remarks/>
[System.Xml.Serialization.XmlElementAttribute("ItemsElementName")]
[System.Xml.Serialization.XmlIgnoreAttribute()]
public ItemsChoiceType[] ItemsElementName
{
get { return this.itemsElementNameField; }
set { this.itemsElementNameField = value; }
}
}
Since these all get generated to partial classes, I am free to add additional code. I need to be able to read/set the individual properties, such as Name, Address, City, etc.
I need to be able to serialize these objects to match the schema.
Is there a way in c# to create a public property that will read or set the value in the Items array at the proper sequence, etc.? ie:
public partial class LocationType
{
public string Address
{
get
{
// code here to return the correct Items[] element
}
set
{
// code here to set the correct Items[] element
}
}
}