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.