tags:

views:

373

answers:

1

Hello folks:

I ask a question about using XPath function there

But I meet a problem about this exception:

[System.Xml.XPath.XPathException] = {"Namespace Manager or XsltContext needed. This query has a prefix, variable, or user-defined function."}

The xml is as follows:

<x-config>
    <!--user location-->
    <x-list>
      <Country code="TW,UK,MY" />
      <Country code="US,CA,MX" />
      <Country code="IN,PR,VI,IR" />
      <Country code="Others" /> 
    </x-list>

And I use following code to retrieve matches:

XmlNode countryNode = cdnConfig.SelectSingleNode(
    string.Format("x-config/x-list/Country[fn:contains(@code, {0})]", countryCode)
);

I am a newbie of XPath. Could anybody elaborate more on this and give some workaround?

Thanks a lot.

+1  A: 

This SO question should answer it.

In short: The fn prefix in your XPath expression is not recognized. You could supply an instance of XmlNamespaceManager to declare the prefix, but since .NET apparently doesn't have XPath 2.0 support, this won't help.

However, the contains function is already supported by XPath 1.0, so no need for XPath 2.0:

XmlNode countryNode = cdnConfig.SelectSingleNode(
    string.Format("x-config/x-list/Country[contains(@code, '{0}')]", countryCode)
);                                         ↑
dtb
Thanks, our legacy is still upon ASP.NET 2.0. Is there any library which can be downloaed to supply 2.0?
Ricky
I've just tested the code snippet. It works.
dtb
You're rigth! It works like a magic.
Ricky