XPathSelectElement always returns null

后端 未结 4 740
借酒劲吻你
借酒劲吻你 2020-12-01 21:00

Why is this Xpath not working using XDocument.XPathSelectElement?

Xpath:

//Plugin/UI[1]/PluginPageCategory[1]/Page[1]/Group[1]/CommandRef[2]
<         


        
4条回答
  •  误落风尘
    2020-12-01 21:24

    The easiest way in your case is to use XPath axes and node test for node name and position to select the element. Your XPath selection:

    myXDocument.XPathSelectElement("//Plugin/UI[1]/PluginPageCategory[1]/Page[1]/Group[1]/CommandRef[2]", myXDocument.Root.CreateNavigator());
    

    Can be easily translate to:

    myXDocument.XPathSelectElement("/child::node()[local-name()='Plugin']/child::node()[local-name()='UI'][position()=1]/child::node()[local-name()='PluginPageCategory'][position()=1]/child::node()[local-name()='Page'][position()=1]/child::node()[local-name()='Group'][position()=1]/child::node()[local-name()='CommandRef'][position()=2]");
    

    There is no need to create and pass XmlNamespaceManager as parameter.

提交回复
热议问题