【问题标题】:XPath in C# code behind of WPFWPF背后的C#代码中的XPath
【发布时间】:2010-09-18 14:25:25
【问题描述】:

如果您在 XAML 中绑定 XML 文档,您可以使用 XPath,但是如果您在后面的代码中动态加载 XML 文档怎么办?后面的 C# 代码中是否有任何可用的 XPath 方法?

(使用 .NET 3.5 SP1)

【问题讨论】:

    标签: c# wpf xml xaml xpath


    【解决方案1】:

    在您的代码中将 XML 加载到 XPathDocument 中,并使用 XPathNavigator 来保存您的查询。 XPathNavigator.Select() 的结果是返回选定节点的迭代器。

    示例(使用 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.
    }
    

    【讨论】:

      猜你喜欢
      • 2015-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-13
      • 2011-02-02
      • 2013-06-03
      • 1970-01-01
      • 2013-05-12
      相关资源
      最近更新 更多