Ever want to work out what the Xpath statement would be for a current node of an XML document? Well here you go. This little tid bit will quickly work its way back up the parent-grandparent tree, up to the root node of the document. And when it's done, you'll be returned the Xpath statement.
Code: (generated for InfoPath C# managed code)
private string GenerateMatchPath(IXMLDOMNode sourceNode, string xPath)
{
IXMLDOMNode parentNode = sourceNode.parentNode;
while (parentNode != null && parentNode.nodeName != "#document")
{
xPath = parentNode.nodeName + "/" + xPath;
parentNode = parentNode.parentNode; } return "/" + xPath;
}//GenerateMatchPath
Now, why would this be useful?
Suppose you would like to compare the data of a pair of XML documents (docA and docB).
If you used an Xpath query (xmlDoc.selectNodes("//descendant-or-self::node()")) to generate a nodelist of all elements in docA, you could:
- loop through each node from docA
- get its value
- generate the xpath
- use the xpath to get the value from docB
- compare values
- take appropriate action (update docA? update docC?)
[Snippet to follow...]
No comments:
Post a Comment