1

Simplified, I have a an XML like that:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?><root> <data> </data></root>

As you can see, there is space after the root entry tag and a space in the data tag. When I now use it like this:

var xmldoc = new XmlDocument();
xmldoc.Load(stream);

... the document looses both spaces, the one after root entry tag and the one in the data tag.

But If I use it like this:

var xmldoc = new XmlDocument();
xmldoc.PreserveWhitespace = true;
xmldoc.Load(stream);

... then both spaces are preserved.

But according to the documentation (documentation, remarks section), the setting PreserveWhitespace = false, which is default, should keep significant white spaces and get rid of not significant ones. But it cleanes all of them, and the one in data tag is significant. Or do I understand something wrong here?

user1470240
  • 580
  • 3
  • 19

1 Answers1

1

A significant whitespace are spaces between text like "this is an example" but empty tags like you described are not significant and will be removed by the xml parser.

martijn
  • 485
  • 2
  • 9
  • Thanks, I was wrong with understanding 'significant whitespace'. – user1470240 Jul 15 '21 at 05:59
  • ... which leads then to this question: https://stackoverflow.com/questions/68388807/net-xmlserializer-how-to-ensure-white-space-from-object-property-value-as-sign – user1470240 Jul 15 '21 at 06:16
  • no the question must be, why do you need spaces in the xml? – martijn Jul 15 '21 at 06:29
  • to keep the data of the serialized object (the serialized object properties, type of string, have sometimes a space as its value - and other than in XML, it's significant information) – user1470240 Jul 15 '21 at 07:17
  • if the spaces are that important then you should reconsider using xml. every xml parser can display xml different and leave the spaces out in presentation. The xml deserializer and serlializer will take care of filling properties and adding elements. by default a string property in an object (class) will have a null value. if the property is null while being serialized it will be skipped (that is how you create optional elements) if it is initialized with content or a string.empty it will create the element. this also works the other way around. – martijn Jul 15 '21 at 08:14
  • The information medium XML is done with 'XmlSerializer' and - unfortunately is currently used by many independent partners/users of the concerning software. But yes, if I would build it up from scratch, I would really reconsider to use XML ... thanks so far. – user1470240 Jul 28 '21 at 12:14