3

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!

sshirley
  • 239
  • 2
  • 6
  • 19
  • 3
    Good god. Those variable names need to be refactored to be readable! – Tejs May 03 '11 at 21:31
  • Yeah. This was a generated class. Too much trouble really to change the names. If I had written these classes myself, I would have done something a bit more human. :-) – sshirley May 05 '11 at 15:49

3 Answers3

6

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.

Shaun Bowe
  • 9,840
  • 11
  • 50
  • 71
  • And also, you cannot serialize a computed property. – Adi May 03 '11 at 21:17
  • 1
    @Adi why not? To the outside it is not visible if a property is computed or not (at least as far as I know...) – Mario The Spoon May 03 '11 at 21:20
  • Thank you very much!! I was pulling my hair out on this one. So basically the problem was that I had an incomplete accessor? I added the ReadOnly() and it didn't work. Then I added the empty set{} and all was perfect. – sshirley May 04 '11 at 14:15
1

Possible answer see: Serializing private member data

Community
  • 1
  • 1
Mario The Spoon
  • 4,799
  • 1
  • 24
  • 36
0

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; } }

Gurpreet Singh
  • 1,641
  • 3
  • 17
  • 29