XPathSelectElement always returns null

后端 未结 4 710
借酒劲吻你
借酒劲吻你 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:09

    There is a way to do it without any change to the xpath. The solution I've found is to remove the namespace when parsing the XML into the XDocument.

    Here is an exemple:

    var regex = @"(xmlns:?[^=]*=[""][^""]*[""])";
    var myXDocument = XDocument.Parse(Regex.Replace("MyXmlContent", regex, "", RegexOptions.IgnoreCase | RegexOptions.Multiline))
    

    Now that the namespace is gone, it is easyer to manipulate.

提交回复
热议问题