问题
I'm fairly new to working with XSD's and serialization de-serialization, I'm looking for some advice on how to initialise the resulting class object created from a xs:choice element in the XSD, to start here's some info on the XSD and Object class:
Examples taken from a larger XSD, object class.
XSD:
<xs:element name="main-contact">
<xs:complexType>
<xs:sequence>
<xs:choice>
<xs:element name="person">
<xs:complexType>
<xs:group ref="person"/>
</xs:complexType>
</xs:element>
<xs:element name="company">
<xs:complexType>
<xs:group ref="company"/>
</xs:complexType>
</xs:element>
</xs:choice>
<xs:group ref="contact"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:group name="person">
<xs:sequence>
<xs:element name="title" minOccurs="0"/>
<xs:element name="salutation" minOccurs="0"/>
<xs:element name="forename" minOccurs="0"/>
<xs:element name="surname">
</xs:element>
<xs:element name="birthdate" type="xs:date" minOccurs="0"/>
</xs:sequence>
</xs:group>
<xs:group name="company">
<xs:sequence>
<xs:element name="name"/>
<xs:element name="phone">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="fax" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[0-9]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="e-mail" minOccurs="0">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="[a-z | A-Z | 0-9 | . | -]+@[a-z | A-Z | 0-9 | . | -]+[.][a-z | A-Z | 0-9 | . | -]+"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="website" type="xs:string" minOccurs="0">
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="contact">
<xs:sequence>
<xs:element name="phone-private" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="phone-office" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="fax-private" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="fax-office" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="e-mail-private" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="e-mail-office" minOccurs="0" maxOccurs="unbounded"/>
<xs:element name="website" minOccurs="0" maxOccurs="unbounded"/>
</xs:sequence>
</xs:group>
XSD.exe created Object class:
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.33440")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class exportDesignCommissionCustomerMaincontact
{
private object itemField;
private object[] phoneprivateField;
private object[] phoneofficeField;
private object[] faxprivateField;
private object[] faxofficeField;
private object[] emailprivateField;
private object[] emailofficeField;
private object[] websiteField;
[System.Xml.Serialization.XmlElementAttribute("company", typeof(exportDesignCommissionCustomerMaincontactCompany))]
[System.Xml.Serialization.XmlElementAttribute("person", typeof(exportDesignCommissionCustomerMaincontactPerson))]
public object Item
{
get
{
return this.itemField;
}
set
{
this.itemField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("phone-private")]
public object[] phoneprivate
{
get
{
return this.phoneprivateField;
}
set
{
this.phoneprivateField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("phone-office")]
public object[] phoneoffice
{
get
{
return this.phoneofficeField;
}
set
{
this.phoneofficeField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("fax-private")]
public object[] faxprivate
{
get
{
return this.faxprivateField;
}
set
{
this.faxprivateField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("fax-office")]
public object[] faxoffice
{
get
{
return this.faxofficeField;
}
set
{
this.faxofficeField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("e-mail-private")]
public object[] emailprivate
{
get
{
return this.emailprivateField;
}
set
{
this.emailprivateField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("e-mail-office")]
public object[] emailoffice
{
get
{
return this.emailofficeField;
}
set
{
this.emailofficeField = value;
}
}
[System.Xml.Serialization.XmlElementAttribute("website")]
public object[] website
{
get
{
return this.websiteField;
}
set
{
this.websiteField = value;
}
}
}
Where I'm stuck is how exactly i can initialize the 'Maincontact' element and it's sub elements, so far I've got to this point with initializing the class:
export.design = new exportDesign
{
commission = new exportDesignCommission
{
dateofcreation = DateTime.Now,
dateoflastchange = DateTime.Now,
customer = new exportDesignCommissionCustomer
{
maincontact = new exportDesignCommissionCustomerMaincontact
{
//Initialisation in question to go here
}
}
},
};
This may be fairly basic but i can't see how this initialisation would work based on a conditional choice to reflect the XSD choice. Can anyone point me in the right direction?
Thanks and thanks again for reading this far :)
回答1:
All tools for generating code from xsd
are not perfect and for complex xsd's you need to add some code by yourself. I better recommended for you to use xsd2code because of its customizability.
Look at that blog Here Simon Middlemiss answering your question.
来源:https://stackoverflow.com/questions/37278507/xsd-exe-how-to-initialize-a-type-created-from-xschoice