0

I'm hoping someone can help answer this for me, as I've been pulling my hair out all morning trying to track down a solution to this issue.

I have a class that needs to be serialized to XML. The XML Serialization works as long as I'm serializing a simple public property. However, if I have a public property that acts as a getter for a private field that backs it, the public property isn't serialized (despite being decorated with [XmlAttribute()]). I've combed through MSDN and StackOverflow looking for answers, but to no avail. I've mocked up an example below.

[Serializable()]
[XmlRoot("foobar")]
public class FooBar
{
     [XmlAttribute("foo")]
     public string Foo { get; set; }
     private bool bar;
     [XmlAttribute("bar")]
     public string Bar 
     {
          get { return ConvertBoolToYesNo(bar); }
     }

    public FooBar()
    {
         Foo = "foo";
         bar = true;
    } 

    public string ConvertBoolToYesNo(bool boolToConvert)
    {
         if(boolToConvert == true)
              return "yes";
         else
              return "no";
    }

}

This returns <?xml version="1.0" encoding="us-ascii"?><foobar foo="foo" /> when I would expect it to return <?xml version="1.0" encoding="us-ascii"?><foobar foo="foo" bar="yes" />. Any suggestions would be appreciated.

Thanks in advance!

Michael
  • 1,036
  • 1
  • 11
  • 22
  • 1
    You did notice that `bar` is readonly, right? – H H Apr 25 '14 at 17:45
  • 1
    Unrelated to the issue (already answered below) but you don't need the `Serializable` attribute on your classes when working with the `XmlSerializer`. – Chris Sinclair Apr 25 '14 at 17:48
  • @ChrisSinclair Good to know. I appreciate it. This is the first time I'm using the XmlSerializer rather than creating the the XML document myself. I'll take note of that for in the future. – Michael Apr 25 '14 at 17:53
  • Are you limited to `XmlSerializer`? IIRC(?), WCF's XML contract serializer allows to serialize private members and thus solve it without creating setter and thus making property non-read-only. Your other option is to implement `IXmlSerializable` to manage de/serialization of private field yourself. – LB2 Apr 25 '14 at 18:00
  • @LB2 Unfortunately, `XmlSerializer` is necessary in this instance (unless I want to handle building the XML document manually, which I'd rather avoid. However, allowing them to be set won't hurt anything - it just wasn't necessary given the scope, so I was avoiding doing so. – Michael Apr 25 '14 at 19:30
  • possible duplicate of [Why are properties without a setter not serialized](http://stackoverflow.com/questions/13401192/why-are-properties-without-a-setter-not-serialized) – nawfal Jul 11 '14 at 20:13

2 Answers2

4

Check this answer right here:

Why are properties without a setter not serialized

Seems like it is a serializer limitation (by design) when you have "readonly" properties, try adding a "setter" and it might work.

Community
  • 1
  • 1
Jportelas
  • 646
  • 10
  • 21
  • Thank you! I was so sure it had something to do with the private field, it never occurred to me to look at whether a serializable property could be readonly. – Michael Apr 25 '14 at 17:52
2

I believe that read-only properties could not be seriazlied by XMLSerializer.

Dermen
  • 76
  • 3