Mark wrote:
I just noticed something that seems counter-intuitive to me. By default an
XmlDocument is set with PreserveWhitesp ace=false. This means that
XmlDocument.Loa d() or .LoadXml() will strip/condense non-semantic whitespace.
*But* I just found that if you take that self-same XmlDocument and do either
XmlDocumentFrag ment node = doc.CreateDocum entFragment();
node.InnerXml = "<foo/>\r\n<bar/>";
or
XmlElement node = doc.CreateEleme nt("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
PreserveWhitesp ace 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("<r oot><foo><bar>b az</bar></foo></root>");
doc.Save(Consol e.Out);
Console.WriteLi ne();
Console.WriteLi ne(doc.OuterXml );
doc.PreserveWhi tespace = true;
doc.Save(Consol e.Out);
Console.WriteLi ne();
Console.WriteLi ne(doc.OuterXml );
Output is
<?xml version="1.0" encoding="ibm85 0"?>
<root>
<foo>
<bar>baz</bar>
</foo>
</root>
<root><foo><bar >baz</bar></foo></root>
<?xml version="1.0"
encoding="ibm85 0"?><root><foo> <bar>baz</bar></foo></root>
<root><foo><bar >baz</bar></foo></root>
--
Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/