Why is The XML Serializer appending characters to my XmlAttribute?

c#, xml-serialization

Solution

It's an escape sequence. The _x0020 is actually another escape sequence for a space, so it's trying to escape the escape sequence so it doesn't get confused that you literally want the escape sequence, not the unescape value. So your attribute should look like this:

public class MyClass
{
     [XmlAttribute("ows_Business Unit")]
     public string BusinessUnit { get; set; } 
}

That will serialize the attribute as `ows_Business_x0020_Unit`.

Problem

Here is my property: ``` /// <summary> /// The Business Unit /// </summary> [XmlAttribute("ows_Business_x0020_Unit")] public string BusinessUnit { get; set; } ``` When I call Serialize on the object that has BusinessUnit I get: ``` ows_Business_x005F_x0020_Unit=\"Hi\" ``` Where does the _x005F come from?

Original source