简体   繁体   English

WPF中的XP#代码

[英]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? 如果您在XAML中绑定XML文档,则可以使用XPath,但如果您在后面的代码中动态加载XML文档会怎么样? Is there any XPath methods available in the C# code behind? C#代码背后是否有可用的XPath方法?

(using .NET 3.5 SP1) (使用.NET 3.5 SP1)

Load the XML into a XPathDocument in your code behind, and use an XPathNavigator to hold your query. 将XML加载到代码后面的XPathDocument中,并使用XPathNavigator来保存查询。 The result of the XPathNavigator.Select() is an iterator that returns the selected nodes. XPathNavigator.Select()的结果是一个返回所选节点的迭代器。

Example (using System.XML and System.Xml.XPath): 示例(使用System.XML和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.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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