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/