Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
 

Go Back   XSL - XML - RSS Forums > XML General > .NET and XML

Tags:



Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 10-23-2008, 11:41 PM
Mark
 
Posts: n/a

Default InnerXml and PreserveWhitespace



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


Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 10-24-2008, 02:18 PM
Martin Honnen
 
Posts: n/a

Default Re: InnerXml and PreserveWhitespace

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/
Reply With Quote
  #3 (permalink)  
Old 10-24-2008, 03:48 PM
Mark
 
Posts: n/a

Default 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:

> 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/
>

Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



Contact Us -|- XSL - XML - RSS Forums -|- Archive -|- Top -|-Rules/Disclaimer-|-Help/Support -|-Advertise
© Camley Interactive (camley.info) 2008 - all logos and images are copywrite their respective owners.
Proud member of the Camley Interactive Network
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0 ©2007, Crawlability, Inc.
All times are GMT. The time now is 04:05 PM.
Style Developed by Epic Designz