Ok, I found a workaround.
It turns out that XmlWriter doing the right thing if there really are any spaces in the xml:space="preserve" block xml:space="preserve" - itβs only when there is no one to screw it and add some, And itβs convenient if there are some white nodes, even if they are empty. So the trick I came up with is to decorate the document with extra spaces of length 0 in the appropriate places before trying to write it down. The result is exactly what I want: to print fairly widely everywhere, except when the space value is significant.
The workaround is to change the indoor unit to:
PreserveWhitespace(doc.DocumentElement); doc.DocumentElement.WriteTo(writer);
...
private static void PreserveWhitespace(XmlElement root) { var nsmgr = new XmlNamespaceManager(root.OwnerDocument.NameTable); foreach (var element in root.SelectNodes("//*[@xml:space='preserve']", nsmgr) .OfType<XmlElement>()) { if (element.HasChildNodes && !(element.FirstChild is XmlSignificantWhitespace)) { var whitespace = element.OwnerDocument.CreateSignificantWhitespace(""); element.InsertBefore(whitespace, element.FirstChild); } } }
I still think this XmlWriter behavior is a bug.
source share