views:

22

answers:

1

For some reason, my XmlElements with spaces in them are printed with _x0020 in place of the space.

For example,

[XmlElement("The Total")]
public double total { get; set; }

turns into <The_0x0020_Total> when I print it out. I'm using a TextWriter to output, then I use XmlSerializer.Serialize to print to a file, but it's not working.

+4  A: 

That's the XML serializer's way of handling the fact that you've specified an illegal element name. XML elements can't have spaces in their names, so it's munging the space in a way that it'll be able to un-munge later.

I would advise you not to include spaces in the first place.

Out of interest, what did you expect the output to be? Did you expect:

<The Total>
  20
</The Total>

?

Jon Skeet
Yes that's what I wanted. If that's not standard XML compliant, then that's all I needed to know.
codersarepeople
@codersarepeople: Yup, that would be invalid XML.
Jon Skeet