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; } }
精彩评论