Connecting Tech Pros Worldwide Forums | Help | Site Map

InnerXml and PreserveWhitespace

=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a
#1: Oct 23 '08
Hi...

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?

Thanks
Mark



Joe Fawcett
Guest
 
Posts: n/a
#2: Oct 24 '08

re: InnerXml and PreserveWhitespace




"Mark" <mmodrall@nospam.nospamwrote in message
news:6895BFB9-C5E1-45DF-AF83-CE9246217535@microsoft.com...
Quote:
Hi...
>
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?
>
Thanks
Mark
>
>
The whole thing is odd. The standards state that preserve is the default,
this is one of the few areas that Microsoft deviate from the standard in
regards to the Xml DOM.

--

Joe Fawcett (MVP - XML)
http://joe.fawcett.name

Martin Honnen
Guest
 
Posts: n/a
#3: Oct 24 '08

re: InnerXml and PreserveWhitespace


Mark wrote:
Quote:
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/
=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a
#4: Oct 24 '08

re: InnerXml and PreserveWhitespace


Thanks for the reply, Martin...

The interesting wrinkle in your sample is that .Save() actually injects
whitespace when PreserveWhitespace = false. The original string you parsed
had no non-semantic whitespace in it, yet the first .Save() applied
formatting to add some.

My next experiment was going to try making an XmlTextReader using
XmlParserContext to see how that interacted with whitespace...

Mark


"Martin Honnen" wrote:
Quote:
Mark wrote:
>
Quote:
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/
>
=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a
#5: Oct 24 '08

re: InnerXml and PreserveWhitespace


Okay, I did find that you can get a fragment without whitespace using
XmlTextReader.

Interestingly, setting XmlParserContext with various XmlSpace values doesn't
seem to change the behavior of the reader at all.

*But* you can fiddle the XmlTextReader.WhitespaceHandling value after
construction to change how it reads things.

I was intrigued by the WhitespaceHandling.None option (separate from the
..Significant option); some whitespace removal would materially change the
meaning of the document, so it seemed like an odd extra to be able to say
"rip it out even if it changes the underlying meaning"...

Thanks
Mark

Closed Thread