简体   繁体   中英

Missing fields when serializing .NET object

I am having a problem with serializing an object in C#. When the application goes to serialize the object, certain fields get serialized but others do not. In the following code:

/// <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);
        }
    }
}

you can see that the Fields/Accessors RqUID and CurCd get called but TransactionRequestDtString and TransactionEffectiveDtString do not. I need all of these to be serialized. Thanks!

If they need to be xml serialized they need a public get and set.

Try changing your code to this:

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

The ReadOnly attribute will not let anyone change it.

Possible answer see:Serializing private member data

I was facing the same issue with some properties(which are nullable), i FIXED it by using: [XmlElement(IsNullable = true)] decorator

public class Person {

[XmlElement(IsNullable = true)] public string Name { get; set; } }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM