0

I do XML serialization for objects with the XmlSerializer. When an object property of type string contains a white space (" "), this value is serialized as insignificant white space:

<tag> </tag>

However, I like to ensure, that this whitespace(s) are kept as significant white spaces. Say, that it would look like:

<tag>&nbsp;</tag>

or:

<tag xml:space="preserve"> </tag>

How can I achieve that with the XmlSerializer?

user1470240
  • 580
  • 3
  • 19
  • Maybe the [answer](https://stackoverflow.com/questions/33152837/preserve-whitespace-only-element-content-when-deserializing-xml-using-xmlseriali) from **Panagiotis Kanavos** will help you. – Philipp Ape Jul 15 '21 at 06:22
  • @PhilippApe: I guess, you mean the possiblity to decorate class members with XML serialization directives. Unfortunately, the objects which I'm serializing, are generic, so I cannot add any serializiation information on object's class members ... – user1470240 Jul 15 '21 at 06:43
  • 1
    Another solution is to override the behavior of your `XmlWriter` (supplied for your `XmlSerializer`), see [here](https://stackoverflow.com/questions/26017833/serializing-options-for-non-breaking-spaces). – Philipp Ape Jul 15 '21 at 06:57
  • @Philipp Ape: Thanks for this hint. Yes I was able to adjust the XML export with XMLSerializer when using a XmlWriter. It worked ok for spaces when I used the XmlWriter to write an entity instead of a real space character, but it turned out, that I'm unable to preserve carriage return and/or line feed even if I use an entity, they always got lost. Currently I read the XML with explicit preserving spaces: not very ellegant, but that works ... – user1470240 Jul 28 '21 at 12:50

0 Answers0