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

时间:2017-05-23 03:15:21

标签: c# xml xsd.exe

我的xsd.exe生成的类并没有完全按照我的要求进行序列化。谁能告诉我如何让InterestInProperty出现在序列化版本中?

生成的类太长(14,000行C#)要在这里发布,但我会尝试显示相关的摘录。供参考,here is the full schema

我想要一个块来序列化:

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

但它实际上是序列化的:

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

目前我们假设序列化InterestInProperty的解决方案将适用于ReasonFor

这是ValuationType类,其中包含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,
}

1 个答案:

答案 0 :(得分:1)

您尚未展示如何填充属性,但我猜测您已忽略与未序列化的两个属性相关的xxxSpecified属性。

the documentation

  

另一种选择是使用特殊模式创建XmlSerializer识别的布尔字段,并将XmlIgnoreAttribute应用于该字段。该模式以 propertyNameSpecified 的形式创建。例如,如果有一个名为“MyFirstName”的字段,您还将创建一个名为“MyFirstNameSpecified”的字段,该字段指示XmlSerializer是否生成名为“MyFirstName”的XML元素。

因此,如果您希望序列化InterestInPropertyReasonFor,则需要将生成的InterestInPropertySpecifiedReasonForSpecified属性设置为true