【问题标题】:Missing fields when serializing .NET object序列化 .NET 对象时缺少字段
【发布时间】:2011-08-18 01:27:20
【问题描述】:

我在用 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 和 TransactionEffectiveDtString 没有。我需要对所有这些进行序列化。谢谢!

【问题讨论】:

  • 天哪。这些变量名需要重构才能可读!
  • 是的。这是一个生成的类。改名字真的太麻烦了。如果我自己编写了这些课程,我会做一些更人性化的事情。 :-)

标签: c# .net serialization xml-serialization


【解决方案1】:

如果需要对它们进行 xml 序列化,则需要公共获取和设置。

尝试将您的代码更改为:

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

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

【讨论】:

  • 而且,你不能序列化一个计算属性。
  • @Adi 为什么不呢?对于外部来说,是否计算属性是不可见的(至少据我所知......)
  • 非常感谢!!我正在把头发拉出来。所以基本上问题是我有一个不完整的访问器?我添加了 ReadOnly() 但它不起作用。然后我添加了空集{},一切都很完美。
【解决方案2】:

可能的答案见:Serializing private member data

【讨论】:

    【解决方案3】:

    我在某些属性(可以为空)方面遇到了同样的问题,我使用以下方法修复了它:[XmlElement(IsNullable = true)] 装饰器

    公共类人 {

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

    【讨论】:

      猜你喜欢
      • 2017-05-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-17
      • 1970-01-01
      • 2023-03-06
      • 2014-01-28
      相关资源
      最近更新 更多