C#   发布时间:2022-04-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了c# – 序列化.NET对象时缺少字段大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用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.datetiR_664_11845@e transactionrequestDtField;

    private System.datetiR_664_11845@e 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.datetiR_664_11845@e 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.datetiR_664_11845@e 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没有.我需要将所有这些序列化.谢谢!

解决方法

如果需要进行xml序列化,则需要公开获取和设置.

尝试将代码更改为:

[ReadOnly(true)]
[XmlElement("transactionrequestDt")]
public String transactionrequestDtString
{
    get
    {
        return String.Format("{0:yyyy-MM-dD}",this.transactionrequestDt);
    }
    set{}
}`

ReadOnly属性不会让任何人改变它.

大佬总结

以上是大佬教程为你收集整理的c# – 序列化.NET对象时缺少字段全部内容,希望文章能够帮你解决c# – 序列化.NET对象时缺少字段所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。