473,382 Members | 1,750 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,382 software developers and data experts.

How to tell if XmlNode is part of document tree

I have a container of XmlNodes. Some of them have been removed from
the document, some haven't. Is there an easy way to tell which are
which?

I suppose
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = ParentNode; }
return node == node.OwnerDocument;
}
would work, but it doesn't seem elegant.

Jan 23 '07 #1
7 2383
ma**********@yahoo.co.uk wrote:
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = ParentNode; }
Should be
while (node.ParentNode != null) { node = node.ParentNode; }
I guess.
return node == node.OwnerDocument;
}
would work, but it doesn't seem elegant.
I don't think there is any method exposed that you could use so that
walk of the ParentNode hierarchy is not too bad.

If you don't want to use the loop you can use XPath e.g.

public static bool IsPartOfDocumentTree (XmlNode node) {
return node.SelectSingleNode(@"ancestor::node()") ==
node.OwnerDocument;
}

--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 23 '07 #2
Martin Honnen wrote:
ma**********@yahoo.co.uk wrote:
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = node.ParentNode; }
[snip correction - thanks!]
return node == node.OwnerDocument;
}
would work, but it doesn't seem elegant.

I don't think there is any method exposed that you could use so that
walk of the ParentNode hierarchy is not too bad.
Thanks. Sigh. (I suppose there isn't much that an API could do, other
than duplicate the loop).
If you don't want to use the loop you can use XPath e.g.

public static bool IsPartOfDocumentTree (XmlNode node) {
return node.SelectSingleNode(@"ancestor::node()") ==
node.OwnerDocument;
}
Hmm. That seems like a sledgehammer to crack a nut. It also feels
hairy applying XPath to something that isn't (part of) a full document.

(On a different topic - I really dislike the way that
SelectNodes/SelectSingleNode ignores the axis ordering. I think I
would make the XPath expression:
"ancestor::node()[last()]"
which ought to accurately select the top of the tree.)

Jan 23 '07 #3
Why are you walking up the parent tree to look for the OwnerDocument? All
descendant nodes, not just the root, reference the parent document. For
instance, the following (assuming my source document had enough children to
return nodes from the XPath) would output the owning document's ToString for
each node:
foreach (XmlNode node in
myDocument.SelectNodes("//*[1]/*[1]/*"))
{
System.Diagnostics.Debug.WriteLine(node.OwnerDocum ent);
}
Jan 23 '07 #4
Keith Patrick wrote:
Why are you walking up the parent tree to look for the OwnerDocument?
Because he needs to find out whether walking up the parent hierarchy he
finds the OwnerDocument as an ancestor or not to check whether the node
he started with is included in the tree rooted by the OwnerDocument or
in an orphan subtree detached from the OwnerDocument.
--

Martin Honnen --- MVP XML
http://JavaScript.FAQTs.com/
Jan 23 '07 #5
On Jan 23, 2:18 pm, martinfro...@yahoo.co.uk wrote:
I have a container of XmlNodes. Some of them have been removed from
the document, some haven't. Is there an easy way to tell which are
which?

I suppose
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = ParentNode; }
return node == node.OwnerDocument;
}
would work, but it doesn't seem elegant.
Oops. There are in fact TWO problems with the above code. Firstly, as
Martin Nonnen points out below, it should be:
node = node.ParentNode;
secondly, the OwnerDocument property of the document node returns null.
So the final test should be:
return (node.NodeType == XmlNodeType.Document);

So, in case somebody wants the full code, it should be:
public static bool IsPartOfDocumentTree (XmlNode node)
{
while (node.ParentNode != null)
{
node = node.ParentNode;
}
return (node.NodeType == XmlNodeType.Document);
}

(Note: The above has still not been compiled, let alone tested!)

Jan 24 '07 #6
I've never had a detached subtree still retain its reference to the original
document, so that's what I'm not seeing. Is he saying that when RemoveNode
is called, the doc reference remains?

Jan 24 '07 #7
Using the Kaysian method for intersection of node-sets the intersection is
non-empty if and only if:
count($ns1[count(.|$ns2) = count($ns2)]) 0

We can use this to tell whether two nodes belong to the same document, by
defining the two node-sets as the sets of ancestors of each of the given
nodes:

Therefore, two nodes belong to the same document if and only if:
count($node1/ancestor::node()[count(.|$node2/ancestor::node()) =
count($node2/ancestor::node()) ]) 0

To answer your specific question, represent the document by the document
node (or by the top element) in the above XPath expression.

This was tested successfully with the XPath Visualizer and it is confirmed
that the above expression returns 'true' only in the case when the two nodes
belong to the same XML document

Cheers,
Dimitre Novatchev

<ma**********@yahoo.co.ukwrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
>I have a container of XmlNodes. Some of them have been removed from
the document, some haven't. Is there an easy way to tell which are
which?

I suppose
bool IsPartOfTree(XmlNode node)
{
while (node.ParentNode != null) { node = ParentNode; }
return node == node.OwnerDocument;
}
would work, but it doesn't seem elegant.

Jan 25 '07 #8

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

Similar topics

1
by: Michael Hutchinson | last post by:
When an XmlNode method returns an XmlNode, such as a child, is this generated on-the-fly or is it the actual component of the internal tree structure? If the parent is moved in the tree, will the...
4
by: Rune | last post by:
I have two queries that appear to be exactly the same, but one of them returns null while the other one returns a valid result! Can anyone provide an explanation to why this is so? Below is an...
1
by: Owen Blacker | last post by:
An awkward question. In a CMS-like environment, I have a list of items that is generated on the serverside using an XmlDocument to throw together XHTML tags (mainly because it's substantially...
5
by: Paul | last post by:
Here I have the definition of an XmlNode which is a property (PayPreference) on my Customer class containing an enum describing how the customer will pay. <PayPerference...
5
by: Mahesh Devjibhai Dhola | last post by:
Hi All, I want to make a custom class in c#, which extends System.Xml.XmlNode class of BCL. Now in custom class, I have implement abstract methods of XmlNode class also. Now when I am trying to...
0
by: Philip Wagenaar | last post by:
I have an XML file that will contain the data of a binary file in MIME encoding. My application receiver the XML file with the location of the binary file: <REQUEST> <IMPORT> <DOCUMENT>...
1
by: Marc | last post by:
Hi! I'm working with a C# client that calls a php web service. I've created a wrapper to call the service using .NET wsdl tool (adding a web reference). The call to the server works fine, it...
7
by: Sharon | last post by:
How can I get the full XML path (as string) of a specific XmlNode ? -- Thanks Sharon
0
by: =?Utf-8?B?ZGF2aWQ=?= | last post by:
When I create a tree view control from XML document, I use XmlNode.Name in the node list iteration. But sometime, it is supposed that it got the element name. But sometimes it got the element name...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.