覆盖的成员没有正确序列化

Overridden member not serialising properly

我的 xsd.exe 生成的 classes 没有按照我希望的方式进行序列化。谁能告诉我如何让InterestInProperty出现在连载版中?

生成的 classes 太长(14,000 行 C#)到 post 这里,但我会尝试显示相关的摘录。作为参考,here is the full schema.

我想要一个块序列化为:

<FullRegistered ValSubType="Standard" ReasonFor="FairMarketValue" InterestInProperty="FeeSimpleInPossession">
    ...
</FullRegistered>

但它实际上是这样序列化的:

<FullRegistered ValSubType="Standard">
    ...
</FullRegistered>

我们暂时假设序列化 InterestInProperty 的解决方案将应用于 ReasonFor

这是 ValuationType class,其中包含 FullRegistered 项:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class ValuationType
{
    private Identifier[] identifierField;
    private object itemField;

    [System.Xml.Serialization.XmlElementAttribute("Identifier")]
    public Identifier[] Identifier
    {
        get { return this.identifierField; }
        set { this.identifierField = value; }
    }

    [System.Xml.Serialization.XmlElementAttribute("Costing", typeof (Costing))]
    [System.Xml.Serialization.XmlElementAttribute("FullRegistered", typeof (FullRegistered))]
    [System.Xml.Serialization.XmlElementAttribute("ProgressInspection", typeof (ProgressInspection))]
    [System.Xml.Serialization.XmlElementAttribute("RestrictedAccessAssessment", typeof (RestrictedAccessAssessment))]
    [System.Xml.Serialization.XmlElementAttribute("WorkFlow", typeof (WorkFlow))]
    public object Item
    {
        get { return this.itemField; }
        set { this.itemField = value; }
    }
}

这是 FullRegistered 的简化定义:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class FullRegistered
{
    private FullRegisteredInterestInProperty interestInPropertyField;
    private bool interestInPropertyFieldSpecified;
    private FullRegisteredValSubType valSubTypeField;
    private FullRegisteredReasonFor reasonForField;
    private bool reasonForFieldSpecified;

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public FullRegisteredInterestInProperty InterestInProperty
    {
        get { return this.interestInPropertyField; }
        set { this.interestInPropertyField = value; }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool InterestInPropertySpecified
    {
        get { return this.interestInPropertyFieldSpecified; }
        set { this.interestInPropertyFieldSpecified = value; }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public FullRegisteredValSubType ValSubType
    {
        get { return this.valSubTypeField; }
        set { this.valSubTypeField = value; }
    }

    [System.Xml.Serialization.XmlAttributeAttribute()]
    public FullRegisteredReasonFor ReasonFor
    {
        get { return this.reasonForField; }
        set { this.reasonForField = value; }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ReasonForSpecified
    {
        get { return this.reasonForFieldSpecified; }
        set { this.reasonForFieldSpecified = value; }
    }

}

FullRegisteredInterestInProperty 枚举:

[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.6.1055.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public enum FullRegisteredInterestInProperty
{
    CrownLeasehold,
    FeeSimpleInPossession,
    ACTLeasehold,
    LeaseholdInterest,
    Lessors,
    Lessees,
    SharesInCompany,
    SubjectToLongTermLease,
    Timeshare,
    UnitsInTrust,
    Other,
}

您没有说明如何填充属性,但我猜您忽略了与两个未序列化的属性相关的 xxxSpecified 属性。

根据 the documentation:

Another option is to use a special pattern to create a Boolean field recognized by the XmlSerializer, and to apply the XmlIgnoreAttribute to the field. The pattern is created in the form of propertyNameSpecified. For example, if there is a field named "MyFirstName" you would also create a field named "MyFirstNameSpecified" that instructs the XmlSerializer whether to generate the XML element named "MyFirstName".

所以,如果你想让InterestInPropertyReasonFor被序列化,你需要将生成的InterestInPropertySpecifiedReasonForSpecified属性设置为true