简体   繁体   中英

XPath in C# code behind of WPF

You can use XPath if you're binding the XML document in the XAML, but what if you're loading the XML document dynamically in the code behind? Is there any XPath methods available in the C# code behind?

(using .NET 3.5 SP1)

Load the XML into a XPathDocument in your code behind, and use an XPathNavigator to hold your query. The result of the XPathNavigator.Select() is an iterator that returns the selected nodes.

Example (using System.XML and System.Xml.XPath):

XPathDocument doc = new XPathDocument(@"c:\filepath\doc.xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = nav.Select("/xpath/query/here");

while(iter->MoveNext)
{
  //Do something with node here.
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM