473,624 Members | 2,005 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

InnerXml and PreserveWhitesp ace

Hi...

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?

Thanks
Mark
Oct 23 '08 #1
4 6793


"Mark" <mm******@nospa m.nospamwrote in message
news:68******** *************** ***********@mic rosoft.com...
Hi...

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?

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

Oct 24 '08 #2
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/
Oct 24 '08 #3
Thanks for the reply, Martin...

The interesting wrinkle in your sample is that .Save() actually injects
whitespace when PreserveWhitesp ace = 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
XmlParserContex t 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 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/
Oct 24 '08 #4
Okay, I did find that you can get a fragment without whitespace using
XmlTextReader.

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

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

I was intrigued by the WhitespaceHandl ing.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

Oct 24 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1254
by: R | last post by:
Hello everybody. I've got problem with preserving whitespaces: $doc->preserveWhiteSpace = false; works fine up to the point when I try to load external XML file. // up to this point whitespaces are OK // load new DomDocument
6
7608
by: Philipp Lenssen | last post by:
Why isn't there a way in standard DOM to access all XML of a node? (Similar to what outerHTML and innerHTML can do on Firefox or Internet Explorer when accessing HTML.) Or am I missing something? -- Google Blogoscoped http://blog.outer-court.com
3
3875
by: Martin Madreza | last post by:
hello, i despair on one simple problem (maybe it's to simple???) I have the string txt = "hallo</hmm>" or txt = "am&24" and want to pass it to the XmlNode InnerXml property myNode.InnerXml = txt //error
3
6568
by: Henrik K | last post by:
Selecting element nodes from a xmlDataDocument using the childNodes collection or by using selectSingleNode and then reading innerXml or outerXml leaks cpu-resources. A trivial example showing this behaviour is included below. Running this example increases the cpu-load gradually and reaches 100% within a minute or two. The problem exists with innerXml as well as outerXml operations. Using a xmlDocument instead of a xmlDataDocument...
1
10179
by: Riko Eksteen | last post by:
Hi I'm reading an xml file into an XmlDocument, adding some nodes, and writing it back out. I would like the nodes I add to assume the same level of indeting as the rest of the document. (I load the document with PreserveWhiteSpace = true.) I thought I could do this by using an XmlTextWriter with Formatting set to Formatting.Indented, but this doesn't work. The Formatting.Indented on the XmlTextWriter only indents my output when I...
3
3942
by: Bas Jaburg | last post by:
I'm creating a Windows App in C#(Smart Client)that calls a webservice. The webservice returns this: <?xml version="1.0" encoding="utf-8" ?> <table id="klasse" records="4792"> <record locator="1"> <KlaKey>04424</KlaKey> <KlaOms>(Niet in gids of internet)</KlaOms> </record> <record locator="2">
0
1179
by: ryang | last post by:
Hi, PreserveWhitesapce property doesn't seem to work for me in various ways. Here are samples that I tried. First the XmlDocument is loaded with PreserveWhitespace = true. XmlDocument xmlDoc = new XmlDocument(); xmlDoc.PreserveWhitespace = true; xmlDoc.Load(validatingReader);
0
1818
by: Shaun Dore | last post by:
Hi, What is the Xml DOM PreserveWhitespace property supposed to do? I thought that setting it to false would remove the superfluous white spaces? For instance, in the following code I would expect the spaces after "for rent" would be stripped, but it's not the case. I know workarounds for this but I'm pretty sure in the old MSXML2.DomDocument this used to work very well. XmlDocument oXml = new XmlDocument(); oXml.PreserveWhitespace =...
2
11950
by: =?Utf-8?B?c2VlbWE=?= | last post by:
What is the differnce between outerXML and innerXML.
0
8236
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8173
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8335
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8475
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7159
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6110
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
1785
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.