Matt_Bro
Matt_Bro

Reputation: 14472

Why is The XML Serializer appending characters to my XmlAttribute?

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?

Upvotes: 4

Views: 478

Answers (2)

Alden
Alden

Reputation: 6703

Microsoft encodes certain characters as _xZZZ_, so any names that look like _xZZZ_ get escaped. They chose to handle this by searching for "_x" and encoding the underscore as _x005F.

Your life will be easier if you avoid including "_x" in any of your names.

Upvotes: 1

vcsjones
vcsjones

Reputation: 141588

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.

Upvotes: 3

Related Questions