C#: XPath to select node with attribute containing a substring?

旧巷老猫 提交于 2019-12-07 13:04:37

问题


Could I use XPath to select country node whose code containing UK?

<country-list>
  <Country code="TW,UK,MY" />
  <Country code="US,CA,MX" />
  <Country code="IN,PR,VI,IR" />
  <Country code="Others" /> 
</country-list>

Thanks.


回答1:


Try the contains() XPath function.

Something like:

/Country[fn:contains(@code, "UK")]

A quick Google search turns up details on XPath functions: http://www.w3schools.com/xpath/xpath_functions.asp




回答2:


You need write it this way:

/country-list/Country[contains(@code,'UK')]



回答3:


You could use Linq to XML - just as an idea

Something like this:

var countryElement = from country in countryElement.GetAttribute("code")
  where country.Value.Contains("UK")
  select countryElement;



回答4:


Yes, do something like

//Country[contains(@code, 'UK')]

which selected the first Country element



来源:https://stackoverflow.com/questions/1781938/c-xpath-to-select-node-with-attribute-containing-a-substring

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