序列化.net对象时缺少字段

本文关键字:字段 net 对象 序列化 | 更新日期: 2023-09-27 17:55:03

我在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);
        }
    }
}

你可以看到字段/访问器RqUID和CurCd被调用,但TransactionRequestDtString和transactioneffective vedtstring没有。我需要把这些都连载起来。谢谢!

序列化.net对象时缺少字段

如果它们需要xml序列化,它们需要一个公共的get和set。

试着这样修改你的代码:

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

ReadOnly属性不允许任何人更改它

可能的答案参见:序列化私有成员数据

我正面临着同样的问题与一些属性(这是可空的),我修复它使用:[XmlElement(IsNullable = true)] decorator

公共类Person{

[XmlElement(IsNullable = true)]公共字符串名称{获取;设置;}}