xdocument save preserve white space inside tags
Asked Answered
B

2

9

I am using XDocument in LINQ to edit (insert) and save xml document.

XDocument doc = XDocument.Load("c:\\sample.xml", LoadOptions.PreserveWhitespace);
doc.Save("c:\\sample.xml",SaveOptions.DisableFormatting)

sample.xml before doc.Save :

<ELEMENT ATTRIB1="attrib1"  ATTRIB2="attrib2" >
    value
</ELEMENT>

sample.xml after doc.Save

<ELEMENT ATTRIB1="attrib1" ATTRIB2="attrib2">
    value
</ELEMENT>

As you can see, there is double space after ATTRIB1 and a single space after ATTRIB2 in the original document. But these spaces have been removed by linq when I call doc.save.

How can I preserve the whitespaces inside tag?

Bush answered 22/12, 2011 at 6:59 Comment(1)
That is the requirement......Bush
A
16

I believe that LoadOptions.PreserveWhitespace and SaveOptions.DisableFormatting only instruct XDocument on how to handle whitespace in terms of indentation and the content of text nodes. It would still normalize the attributes, etc.

You may wish to use an overload where you specify an XmlWriter that is configured to do what you want, and if you can't find a configuration that works with the default XmlTextWriter, you could always create your own XmlWriter.

Astray answered 22/12, 2011 at 7:12 Comment(0)
U
1

These are "not significant whitespaces" and are removed at the moment of reading the XML. By the time you call save there is no information about spacing between attributes. (Note that strictly speaking even order of attributes may not be known as it has no significance in XML).

If you want to read/write XML in a way that is not directly supported by XML standard you need to provide some custom handling. Depending on requirements custom XmlWriter may be enough (i.e. if you want uniformly separate attributes with 2 whitespaces) or you'll need to build whole stack (readers/writers/nodes) yourself if you want to actually preserve information from original XML (treating it as text, not XML).

Uproarious answered 22/12, 2011 at 9:7 Comment(4)
That's doesn't seem correct. I believe it's reformatted during the save, not the read. And even if that were true, then the documentation is literally lying; you'll notice the documentation for LoadOptions.PreserveWhitespace reads "Preserves insignificant white space while parsing." and the documentation for SaveOptions.DisableFormatting reads "Preserve all insignificant white space while serializing.".Grandparent
Also the comment about this XML not being supported by the standard is out of place, if it was not supported by the standard, it would not be valid XML, and an exception would be thrown at parsing time.Grandparent
@Grandparent - Note that from XML point of view significant/insignificant whitespace is only considered inside content of element (not in start tag where attributes are) - so 2 spaces between attributes are not considered "whitespace" but rather just syntax separator for attributes. So I'm not sure why do you think PreserveWhitespace should have impact on how spaces between attributes are handled. Check out w3.org/TR/2000/REC-xml-20001006#sec-white-space for more formal definition.Uproarious
@Grandparent I don't see where I said "this XML not being supported by standard"... There is sentence about "...write XML in a way not directly supported by standard..." (standard essentially let one write attributes whatever way they want as long as attributes are inside start tag and separated by some space characters), but I don't think you should read it as "write XML not supported by standard".Uproarious

© 2022 - 2024 — McMap. All rights reserved.