Asp.net XPath属性查询和检索

Asp.net XPath属性查询和检索,asp.net,xml,xpath,Asp.net,Xml,Xpath,我一直很难理解XPath 我想做的是从货币国家代码中提取一个汇率 我的带有currecies的XML如下所示: 这是我的疑问: query = xml.XPathSelectElement(**"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"**).Value; 我在.NET 3.5和asp webservice项目中工作,该项目产生以下错误: System.Xml.XPath.XPathException: 'exh

我一直很难理解XPath

我想做的是从货币国家代码中提取一个汇率

我的带有currecies的XML如下所示:

这是我的疑问:

query = xml.XPathSelectElement(**"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"**).Value;
我在.NET 3.5和asp webservice项目中工作,该项目产生以下错误:

System.Xml.XPath.XPathException: 'exhangerates/dailyrates/currency[@code='USD']@rate' has an invalid token.
   at MS.Internal.Xml.XPath.XPathParser.ParseXPathExpresion(String xpathExpresion)
   at MS.Internal.Xml.XPath.QueryBuilder.Build(String query, Boolean allowVar, Boolean allowKey)
   at MS.Internal.Xml.XPath.QueryBuilder.Build(String query, Boolean& needContext)
   at System.Xml.XPath.XPathExpression.Compile(String xpath, IXmlNamespaceResolver nsResolver)
   at System.Xml.XPath.XPathNavigator.Evaluate(String xpath, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.XPathEvaluator.Evaluate[T](XNode node, String expression, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.Extensions.XPathSelectElements(XNode node, String expression, IXmlNamespaceResolver resolver)
   at System.Xml.XPath.Extensions.XPathSelectElement(XNode node, String expression)
   at WebServiceCurrency.Service1.RetrieveExchangeRate(String country) in c:\Users\Peter\Documents\GitHub\HotMess\C#\Webservices\WebServiceCurrency\WebServiceCurrency\Service1.asmx.cs:line 35
这项工作:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>    
 <xsl:template match="/">

   <xsl:value-of select="//dailyrates/currency[@code='USD']/@rate" />

 </xsl:template>
</xsl:stylesheet>
所以你的解决方案应该是

query = xml.XPathSelectElement(**"exhangerates/dailyrates/currency[@code='"+ country +"']/@rate"**).Value;

错误消息试图告诉您XPath有语法错误。为了

"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"
阅读


也就是说,在导航到
currency
节点的步骤和导航到其
rate
属性的后续步骤之间插入一条斜线。(我看到PCM在这个解决方案上击败了我。脱帽致敬。)

我忘了提到,我想要rate属性,条件是国家代码。我无法找到具有多个属性的示例。我使用了PCM的表达式,但仍然收到以下错误:System.Web.Services.Protocols.SoapException:服务器无法处理请求。-->System.InvalidOperationException:XPath表达式计算为意外类型System.Xml.Linq.XAttribute。在System.Xml.XPath.xpatheevaluator.d__0`1.MoveNext()中,您调用一个方法,该方法表示它选择并返回一个元素,并将一个选择属性的XPath表达式传递给它。试着寻找另一种方法?(也许XPathEvaluate可以满足您的需要?也许首先使用XPathSelectElement选择货币元素,然后在调用XPathEvaluate时使用该元素作为上下文节点计算表达式“/@rate”)。在任何情况下,阅读错误消息就像他们试图告诉你一些事情一样。是的,好吗?为什么要贴好建议,用屈尊的废话?我评论了一个正在运行的一行程序。对你没有分数,对不起,冒犯了你;没有居高临下的意图。现在,您已经两次询问如何解决一个问题,当您报告的错误消息使问题的性质变得清晰,并且从问题的性质中得出一个明显的解决方案时。在我看来,您似乎没有仔细查看错误消息;如果你想从别人那里得到帮助,你应该看起来像是在付出一些努力。result=float.Parse(xml.SelectSingleNode(“//dailyrates/currency[@code=”“+countryCode+”]/@rate”).Value);
"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"
"exhangerates/dailyrates/currency[@code='"+ country +"']/@rate"