Отсутствующие поля при сериализации объекта .NET - PullRequest
3 голосов
/ 04 мая 2011

У меня проблема с сериализацией объекта в C #.Когда приложение выполняет сериализацию объекта, некоторые поля сериализуются, а другие нет.В следующем коде:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class ACORDInsuranceSvcRqHomePolicyQuoteInqRq
{

    private string rqUIDField;

    private System.DateTime transactionRequestDtField;

    private System.DateTime transactionEffectiveDtField;

    private string curCdField;

    /// <remarks/>
    public string RqUID
    {
        get
        {
            return this.rqUIDField;
        }
        set
        {
            this.rqUIDField = value;
        }
    }

    /// <remarks/>
    public string CurCd
    {
        get
        {
            return this.curCdField;
        }
        set
        {
            this.curCdField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnore()]
    public System.DateTime TransactionRequestDt
    {
        get
        {
            return this.transactionRequestDtField;
        }
        set
        {
            this.transactionRequestDtField = value;
        }
    }

    /// <remarks/>
    [XmlElement("TransactionRequestDt")]
    public string TransactionRequestDtString
    {
        get
        {
            return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnore]
    public System.DateTime TransactionEffectiveDt
    {
        get
        {
            return this.transactionEffectiveDtField;
        }
        set
        {
            this.transactionEffectiveDtField = value;
        }
    }

    /// <remarks/>
    [XmlElement("TransactionEffectiveDt")]
    public string TransactionEffectiveDtString
    {
        get
        {
            return String.Format("{0:yyyy-MM-dd}", this.TransactionEffectiveDt);
        }
    }
}

вы видите, что Fields / Accessors RqUID и CurCd вызываются, а TransactionRequestDtString и TransactionEffectiveDtString - нет.Мне нужно, чтобы все это было сериализовано.Спасибо!

Ответы [ 3 ]

6 голосов
/ 04 мая 2011

Если они должны быть сериализованы в xml, они должны получить и установить публично.

Попробуйте изменить свой код на это:

[ReadOnly(true)]
[XmlElement("TransactionRequestDt")]
public string TransactionRequestDtString
{
    get
    {
        return String.Format("{0:yyyy-MM-dd}", this.TransactionRequestDt);
    }
    set{}
}`

Атрибут ReadOnly не позволит никому изменить его.

1 голос
/ 04 мая 2011
0 голосов
/ 02 апреля 2014

Я столкнулся с той же проблемой с некоторыми свойствами (которые можно обнулять), я ИСПРАВЛЕН с помощью: [XmlElement (IsNullable = true)] decorator

человек публичного класса {

[XmlElement (IsNullable = true)] публичная строка Name {get; задавать; } }

...