Connecting Tech Pros Worldwide Help | Site Map

InnerXml and PreserveWhitespace

  #1  
Old October 23rd, 2008, 11:25 PM
=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a
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


  #2  
Old October 24th, 2008, 10:15 AM
Joe Fawcett
Guest
 
Posts: n/a

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

  #3  
Old October 24th, 2008, 02:15 PM
Martin Honnen
Guest
 
Posts: n/a

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/
  #4  
Old October 24th, 2008, 02:55 PM
=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a

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/
>
  #5  
Old October 24th, 2008, 05:15 PM
=?Utf-8?B?TWFyaw==?=
Guest
 
Posts: n/a

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


Similar Threads
Thread Thread Starter Forum Replies Last Post
XPathNavigator.SelectSingleNode(xpath) on space returns 0 length s David Thielen answers 12 June 27th, 2008 08:20 PM
Empty XmlElements creating new line Zief answers 3 November 7th, 2006 03:15 PM
Parsing XML file ttomes answers 5 May 11th, 2006 07:35 AM
Example Code - Python+PythonNet, Ironpython, Boo srijit@yahoo.com answers 3 July 18th, 2005 05:32 PM