title: XML Serialization of public property backing private field in C#
body: <p>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.</p>
  
  <p>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 <strong>public</strong> property that acts as a getter for a <strong>private field</strong> that backs it, the public property isn't serialized (despite being decorated with <code>[XmlAttribute()]</code>). I've combed through MSDN and StackOverflow looking for answers, but to no avail. I've mocked up an example below.</p>
  
  <pre><code>[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";
      }
  </code></pre>
  
  <p>}</p>
  
  <p>This returns <code>&lt;?xml version="1.0" encoding="us-ascii"?&gt;&lt;foobar foo="foo" /&gt;</code> when I would expect it to return <code>&lt;?xml version="1.0" encoding="us-ascii"?&gt;&lt;foobar foo="foo" bar="yes" /&gt;</code>. Any suggestions would be appreciated. </p>
  
  <p>Thanks in advance!</p>