简体   繁体   English

如何在C#中选择带有XPath的节点?

[英]How to select nodes with XPath in C#?

Simple question, I just want to select the text from the <Template> tag. 简单的问题,我只想从<Template>标签中选择文本。 Here's what I have, but the Xpath doesn't match anything. 这就是我所拥有的,但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);
    }
}

You need to use an XmlNamespaceManager because the Template element is in a namespace: 您需要使用XmlNamespaceManager因为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);
}

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);
}

Here your xpath expression requires a namespace resolution. 这里xpath表达式需要命名空间解析。 you have to instanciate a XmlNamespaceManager and use it in your SelectNodes. 您必须实例化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);

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. 你的选择会奏效。

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

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