How to select nodes where node name contains “mystring”

久未见 提交于 2019-12-07 03:24:16

问题


I need to get XmlNodeList where node name contains "mystring"

XML

    <?xml version="1.0" encoding="utf-8"?>
<root>
  <node1>
    node1 value
  </node1>
  <node2_mystring>
    node2 value
  </node2_mystring>
  <node3>
    node3 value
  </node3>
  <node4_mystring>
    node 4 value
  </node4_mystring>
</root>

Desired output is

<?xml version="1.0" encoding="utf-8"?>
<root>
  <node2_mystring>
    node2 value
  </node2_mystring>
  <node4_mystring>
    node 4 value
  </node4_mystring>
</root>

I tried something like XmlNodeList mystringElements = xmlDocument.SelectNodes(@"//*[contains(name,'mystring')]");

But it returns zero node. What should I put in XPath query to achieve this.


回答1:


You need to use the name() function. Just name alone will try to match an element named "name".

You want this:

//*[contains(name(),'mystring')]


来源:https://stackoverflow.com/questions/2691426/how-to-select-nodes-where-node-name-contains-mystring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!