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!