Doc-O-Matic Live Demo / Microsoft Namespace / Microsoft.Xml Namespace / Classes / XPathGenerator
XPathGenerator
Close
domv10-banner-200
XPathGenerator Class

Namespace: Microsoft.Xml

The XPathGenerator takes an XmlNode object, and an XmlNamespaceManager as follows:

<line><span style='color:teal'>XmlDocument</span> doc = <span style='color:blue'>new</span>  <span style='color:teal'>XmlDocument</span>();</line> <line>doc.Load(<span style='color:maroon'>"test.xml"</span>);</line> <line><span style='color:teal'>XmlNode</span> node = doc.DocumentElement.FirstChild.LastChild;</line> <line> </line> <line><span style='color:teal'>XmlNamespaceManager</span><span> nsmgr = <span style='color:blue'>new</span> <span style='color:teal'>XmlNamespaceManager</span>(doc.NameTable);</span></line> <line><span style='color:teal'>XPathGenerator</span> gen = <span style='color:blue'>new</span> <span style='color:teal'>XPathGenerator</span>();</line> <line><span style='color:blue'>string</span><span > xpath = gen.GetXPath(node, nsmgr);</span></line>

The resulting string can then be used in SelectNodes or SelectSingleNodes using the same XmlNamespaceManager and it will find the same node as follows:

<line><span style='color:teal'>XmlNode</span> found = doc.SelectSingleNode(xpath, nsmgr); </line> <line><span style='color:teal'>Debug</span>.Assert(found == node);</line>
C++
C#
Visual Basic
IDL
JavaScript
class XPathGenerator;
Microsoft.Xml.XPathGenerator
Copyright © 2020. All rights reserved.