XPath 多个重复的匿名命名空间

XPath multiple, duplicate anonymous namespaces

我需要处理 XML 文件中的数据,类似于:

 <?xml version="1.0" encoding="utf-8"?>
    <bpr:release xmlns:bpr="http://www.bp.co.uk/product/release">
        <bpr:contents>
            <process id="1" name="Process 1" xmlns="http://www.bp.co.uk/product/process">
                <process name="Process 1"></process>
            </process>
            <process id="2" name="Process 2" xmlns="http://www.bp.co.uk/product/process">
                <process name="Process 1"></process>
            </process>
            <object id="1" name="Object 1" xmlns="http://www.bp.co.uk/product/process">
                <process name="Object 1" ></process>
            </object>
            <work-queue id="1" name="Queue1" xmlns="http://www.bp.co.uk/product/work-queue"/>
            <process-group id="1" xmlns="http://www.bp.co.uk/product/process-group">
                <members>
                    <process id="1"/>
                    <process id="2"/>
                </members>
            </process-group>
            <object-group id="1" xmlns="http://www.bp.co.uk/product/object-group">
                <members>
                    <object id="1"/>
                    <object id="2"/>
                </members>
            </object-group>
        </bpr:contents>
    </bpr:release>

例如,我需要提取 /bpr:release/bpr:contents/process/process 节点进行分析,但我得到空值或此错误:

System.Xml.XPath.XPathException
  HResult=0x80131943
  Message=Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function.
  Source=System.Xml

我看到该文件有多个匿名命名空间,并且同一个命名空间被使用了两次。我尝试了各种组合,例如:

XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(xmlPath);
var nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);
nsmgr.AddNamespace("bpr", "http://www.bp.co.uk/product/release");
nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpo", "http://www.bp.co.uk/product/process");
nsmgr.AddNamespace("bpwq", "http://www.bp.co.uk/product/work-queue");
nsmgr.AddNamespace("bppg", "http://www.bp.co.uk/product/process-group");
nsmgr.AddNamespace("bpog", "http://www.bp.co.uk/product/object-group");
XmlNodeList p;
XmlNodeList r;
try { r = xmlDoc.SelectNodes("/bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("//bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("bpr:release"); } catch { }
try { r = xmlDoc.SelectNodes("/release"); } catch { }
try { r = xmlDoc.SelectNodes("//release"); } catch { }
try { r = xmlDoc.SelectNodes("release"); } catch { }
try { p = xmlDoc.SelectNodes("process"); } catch { }
try { p = xmlDoc.SelectNodes("bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("//process"); } catch { }
try { p = xmlDoc.SelectNodes("//bpp:process"); } catch { }
try { p = xmlDoc.SelectNodes("/bpr:release/bpr:contents/process/process"); } catch { }

获取 bpr:releasebpr:contentsprocess/processobject/process 等的每个子节点的正确 XPath 是什么?如何为此使用 XMLNamespaceManager

您正在向您的 XmlNamespaceManager 添加名称空间前缀声明,但您没有在对 SelectNodes().

的调用中使用 nsmgr
try { r = xmlDoc.SelectNodes("/bpr:release", nsmgr); } catch { }
                                           ^^^^^^^

相应地修复其他调用。

另请注意,您的第一个示例 XPath,

/bpr:release/bpr:contents/process/process

应该是

/bpr:release/bpr:contents/bpp:process/bpp:process

根据您的声明:

nsmgr.AddNamespace("bpp", "http://www.bp.co.uk/product/process");

最后,您应该删除某些 AddNamespace() 调用中的重复前缀声明(例如 bpp/bpo)。

另见