Question: I'm sending xml to another program, which expects boolean flags as "yes" or "no", rather than "true" or "false".

I have a class defined like:

```
[XmlRoot()]
public class Foo {
    public bool Bar { get; set; }
}

```

When I serialize it, my output looks like this:

```
<Foo><Bar>true</Bar></Foo>

```

But I would like it to be this:

```
<Foo><Bar>yes</Bar></Foo>

```

Can I do this at the time of serialization? I would prefer not to have to resort to this:

```
[XmlRoot()]
public class Foo {
    [XmlIgnore()]
    public bool Bar { get; set; }

    [XmlElement("Bar")]
    public string BarXml { get { return (Bar) ? "yes" : "no"; } }
}

```

Note that I also want to be able to *deserialize* this data back again.

Answer: 
Very simple. Use a surrogate property. Apply XmlIgnore on the actual property. The surrogate is a string, and must use the XmlElement attribute that takes a element-name override. Specify the name of the *actual property* in the override. The surrogate property serializes differently based on the value of the actual property. You must also provide a setter for the Surrogate, and the setter should set the actual property appropriately, for whatever value it serialized. In other words it needs to go both ways. 

Snip:

```
    public class SomeType 
    {

        [XmlElement]
        public int IntValue;

        [XmlIgnore]
        public bool Value;

        [XmlElement("Value")]
        public string Value_Surrogate {
            get { return (Value)? "Yes, definitely!":"Absolutely NOT!"; }
            set { Value= (value=="Yes, definitely!"); }
        }

    }

```

click here for [full compilable source example](http://cheeso.members.winisp.net/srcview.aspx?dir=xml-serialization&file=Surrogate.cs).
@Blorgbeard:
If you have more then one of these YesNo classes in an object class,
make sure to read the entire element.

```
public void ReadXml(XmlReader reader)
{
    string element = reader.ReadOuterXml();
    int startIndex = element.IndexOf('>') + 1;
    int length = element.LastIndexOf('<') - startIndex;

    string text = (element.Substring(startIndex, length).ToLowerInvariant();

    Value = (text == "yes");
}

```

Otherwise this might cause problems.

> 
> The ReadXml method must reconstitute your object using the information that was written by the WriteXml method.
> 
> 
> When this method is called, the reader is positioned at the start of the element that wraps the information for your type. That is, just
>  before the start tag that indicates the beginning of a serialized
>  object. When this method returns, it must have read the entire element
>  from beginning to end, including all of its contents. Unlike the
>  WriteXml method, the framework does not handle the wrapper element
>  automatically. Your implementation must do so. Failing to observe
>  these positioning rules may cause code to generate unexpected runtime
>  exceptions or corrupt data.
> 
> 
>