Mark wrote:
> I just noticed something that seems counter-intuitive to me. By default an
> XmlDocument is set with PreserveWhitespace=false. This means that
> XmlDocument.Load() or .LoadXml() will strip/condense non-semantic whitespace.
>
> *But* I just found that if you take that self-same XmlDocument and do either
> XmlDocumentFragment node = doc.CreateDocumentFragment();
> node.InnerXml = "<foo/>\r\n<bar/>";
>
> or
> XmlElement node = doc.CreateElement("baz");
> node.InnerXml = "<foo/>\r\n<bar/>";
>
> that the whitespace gets preserved, despite the parent document settings.
>
> What's the rationale for this?
I am not sure but OuterXml/InnerXml never pay attention to the
PreserveWhitespace property, neither on reading nor on setting.
Here is an example showing that for reading OuterXml and comparing it to
the Save method:
XmlDocument doc = new XmlDocument();
doc.LoadXml("<root><foo><bar>baz</bar></foo></root >");
doc.Save(Console.Out);
Console.WriteLine();
Console.WriteLine(doc.OuterXml);
doc.PreserveWhitespace = true;
doc.Save(Console.Out);
Console.WriteLine();
Console.WriteLine(doc.OuterXml);
Output is
<?xml version="1.0" encoding="ibm850"?>
<root>
<foo>
<bar>baz</bar>
</foo>
</root>
<root><foo><bar>baz</bar></foo></root>
<?xml version="1.0"
encoding="ibm850"?><root><foo><bar>baz</bar></foo> </root>
<root><foo><bar>baz</bar></foo></root>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/