views:

627

answers:

4

Hi folks:

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  A: 

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;

HTH robert.oh.

robert.oh.
+3  A: 

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

Brannon
thanks for the assistance immediately.
Ricky
Hello, I meet this error:[System.Xml.XPath.XPathException] = {"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."}How could I get around it?
Ricky
It's the 'fn:' namespace prefix. Try removing that.
Brannon
A: 

Yes, do something like

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

which selected the first Country element

HakonB
A: 

Hi

You need write this way,

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

here more about XML and XPath in .NET

Thank you

RRaveen