XSD.exe - 如何初始化从 xs:choice 创建的类型
XSD.exe - How to initialize a type created from xs:choice
我对使用 XSD 和序列化反序列化还很陌生,我正在寻找一些关于如何初始化从 [= 创建的结果 class 对象的建议XSD 中的 32=] 元素,这里是关于 XSD 和对象 class 的一些信息:
示例取自更大的 XSD,对象 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 创建对象 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;
}
}
}
我卡住的地方是我如何准确地初始化 'Maincontact' 元素及其子元素,到目前为止我已经初始化了 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
}
}
},
};
这可能是相当基本的,但我看不出这种初始化如何基于条件选择来反映 XSD 选择。谁能指出我正确的方向?
再次感谢您阅读到这里:)
所有用于从 xsd
生成代码的工具都不是完美的,对于复杂的 xsd,您需要自己添加一些代码。我更推荐你使用 xsd2code 因为它的可定制性。
Look at that blog Simon Middlemiss 在这里回答你的问题。
我对使用 XSD 和序列化反序列化还很陌生,我正在寻找一些关于如何初始化从 [= 创建的结果 class 对象的建议XSD 中的 32=] 元素,这里是关于 XSD 和对象 class 的一些信息:
示例取自更大的 XSD,对象 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 创建对象 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;
}
}
}
我卡住的地方是我如何准确地初始化 'Maincontact' 元素及其子元素,到目前为止我已经初始化了 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
}
}
},
};
这可能是相当基本的,但我看不出这种初始化如何基于条件选择来反映 XSD 选择。谁能指出我正确的方向?
再次感谢您阅读到这里:)
所有用于从 xsd
生成代码的工具都不是完美的,对于复杂的 xsd,您需要自己添加一些代码。我更推荐你使用 xsd2code 因为它的可定制性。
Look at that blog Simon Middlemiss 在这里回答你的问题。