c# – 序列化.NET对象时缺少字段
发布时间:2020-12-16 01:43:40 所属栏目:百科 来源:网络整理
导读:我在使用C#序列化对象时遇到问题.当应用程序进行序列化对象时,某些字段会被序列化,但其他字段则不会.在以下代码中: /// remarks/[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd","2.0.50727.3038")][System.SerializableAttribute()][System.Diagno
我在使用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没有.我需要将所有这些序列化.谢谢! 解决方法
如果需要进行xml序列化,则需要公开获取和设置.
尝试将代码更改为: [ReadOnly(true)] [XmlElement("TransactionRequestDt")] public string TransactionRequestDtString { get { return String.Format("{0:yyyy-MM-dd}",this.TransactionRequestDt); } set{} }` ReadOnly属性不会让任何人改变它. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |