如何在 C# 中使用 XPath 选择节点?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1100156/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 08:13:36  来源:igfitidea点击:

How to select nodes with XPath in C#?

c#.netxpath

提问by Joe

Simple question, I just want to select the text from the <Template> tag. Here's what I have, but the Xpath doesn't match anything.

简单的问题,我只想从 <Template> 标签中选择文本。这是我所拥有的,但 Xpath 不匹配任何东西。

public static void TestXPath()
{
    string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
    xmlText += "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">";
    xmlText += "<Template>Normal</Template>  <TotalTime>1</TotalTime>  <Pages>1</Pages>  <Words>6</Words>";
    xmlText += "</Properties>";

    XmlDocument xmlDoc = new XmlDocument();
    xmlDoc.Load(new System.IO.StringReader(xmlText));

    foreach (XmlNode node in xmlDoc.SelectNodes("//Template"))
    {
        Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
    }
}

采纳答案by Jon Skeet

You need to use an XmlNamespaceManagerbecause the Template element is in a namespace:

您需要使用 anXmlNamespaceManager因为 Template 元素位于命名空间中:

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(new System.IO.StringReader(xmlText));
XmlNamespaceManager manager = new XmlNamespaceManager(xmlDoc.NameTable);
manager.AddNamespace("ns", 
    "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");

foreach (XmlNode node in xmlDoc.SelectNodes("//ns:Template", manager))
{
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}

回答by Marc Gravell

That is a namespace issue; you need to get the name-table, pick an alias, and use that in your query. Or perhaps (in this case) try GetElementsByTagName.

这是一个命名空间问题;您需要获取名称表,选择一个别名,并在您的查询中使用它。或者(在这种情况下)尝试GetElementsByTagName.

XmlNamespaceManager mgr = new XmlNamespaceManager(xmlDoc.NameTable);
mgr.AddNamespace("x",
    "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");
foreach (XmlNode node in xmlDoc.SelectNodes("//x:Template", mgr))
{
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}

Or:

或者:

foreach (XmlNode node in xmlDoc.GetElementsByTagName("Template"))
{
    Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
}

回答by PierrOz

Here your xpath expression requires a namespace resolution. you have to instanciate a XmlNamespaceManager and use it in your SelectNodes.

这里您的 xpath 表达式需要命名空间解析。你必须实例化一个 XmlNamespaceManager 并在你的 SelectNodes 中使用它。

this sample should work

这个样本应该工作

    public static void TestXPath()
    {
        string xmlText = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>";
        xmlText += "<Properties xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\">";
        xmlText += "<Template>Normal</Template>  <TotalTime>1</TotalTime>  <Pages>1</Pages>  <Words>6</Words>";
        xmlText += "</Properties>";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(new System.IO.StringReader(xmlText));

        XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
        nsmgr.AddNamespace("res", "http://schemas.openxmlformats.org/officeDocument/2006/extended-properties");

        foreach (XmlNode node in xmlDoc.SelectNodes("//res:Template", nsmgr))
        {
            Console.WriteLine("{0}: {1}", node.Name, node.InnerText);
        }
    }

you can also get the default namespace by using and write

您还可以通过使用和编写来获取默认命名空间

string s = xmlDoc.DocumentElement.GetNamespaceOfPrefix("");
nsmgr.AddNamespace("ns", s);

回答by Joe

Why do you need the namespace here anyway? just get rid of these

为什么这里需要命名空间?摆脱这些

xmlns=\"http://schemas.openxmlformats.org/officeDocument/2006/extended-properties\" 
xmlns:vt=\"http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes\"

and your selection will work.

您的选择将起作用。