468,101 Members | 1,325 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,101 developers. It's quick & easy.

xmldocument.preservewhite space question

I have an xmldocument and need to set the preservewhitespace to true as
after processing the document I want to write out the xml (using
outerxml/innerxml) and have the identations preserved for readability.

However with the preservewhite space option set code such as:

foreach (XmlNode node in ParentNode)
{

}

fails as the whitespace seems to confuse the parser, how can I work
around this?

Oct 10 '06 #1
1 1458


ro****@orisoft.co.uk wrote:
I have an xmldocument and need to set the preservewhitespace to true as
after processing the document I want to write out the xml (using
outerxml/innerxml) and have the identations preserved for readability.

However with the preservewhite space option set code such as:

foreach (XmlNode node in ParentNode)
{

}

fails as the whitespace seems to confuse the parser, how can I work
around this?
If you preserve white space then white space appears as text nodes in
the DOM meaming if you enumerate the child nodes of an element having
contents of the form e.g.
<parent>
<child>whatever</child>
<child>whatelse</child>
</parent>
then the parent element has five child nodes, the first being a text
node with white space (the white space between the start <parenttag
and the start <childtag), the second being an element node with tag
name 'child', the third a text node with white space, the fourth an
element node with tag name 'child' and the fifth again a text node with
white space.
So there is no confusion I think, the parser finds white space between
tags and parses that white space into text nodes containing the white
space. Thus if you loop through the child nodes or enumerate them then
you need to check node.NodeType == XmlNodeType.Element to find only
element nodes. Or use e.g. node.SelectNodes(@"*") to use XPath to find
the element nodes.

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Oct 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by todd | last post: by
8 posts views Thread by pete | last post: by
reply views Thread by Shaun Dore | last post: by
2 posts views Thread by Glenn Venzke | last post: by
5 posts views Thread by GaryDean | last post: by
1 post views Thread by Solo | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.