XSD.exe - 如何初始化从xs:choice

时间:2016-05-17 14:07:49

标签: c# serialization xsd.exe

我对使用XSD和序列化反序列化相当陌生,我正在寻找一些关于如何初始化从xs:choice元素创建的结果类对象的建议。 XSD,从这里开始有关XSD和Object类的一些信息:

从较大的XSD,对象类中获取的示例。

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创建了Object类:

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

我陷入困境的原因是我如何能够初步确定主要联系人&#39;元素及其子元素,到目前为止,我已经初步化了这个类:

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选择的条件选择起作用。有人能指出我正确的方向吗?

再次感谢并再次感谢您阅读此内容:)

1 个答案:

答案 0 :(得分:1)

所有用于生成xsd代码的工具都不完美,对于复杂的xsd,您需要自己添加一些代码。我建议您使用xsd2code,因为它具有可自定义性。

Look at that blog Simon Middlemiss回答了你的问题。