【问题标题】:XPath attribute query and retrieveXPath 属性查询和检索
【发布时间】:2012-12-18 02:04:22
【问题描述】:

我一直无法理解 XPath。

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

我的带有货币的 XML 在这里:http://www.nationalbanken.dk/dndk/valuta.nsf/valuta.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

【问题讨论】:

  • 我忘了说,我想要 rate 属性,条件是国家代码。我一直找不到具有多个属性的示例。

标签: asp.net xml xpath


【解决方案1】:

这行得通:

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

输出:

566,97

所以你的解决方案应该是

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

【讨论】:

  • result = float.Parse(xml.SelectSingleNode("//dailyrates/currency[@code='" + countryCode + "']/@rate").Value);
【解决方案2】:

您的错误消息试图告诉您您的 XPath 存在语法错误。对于

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

阅读

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

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

【讨论】:

  • 我使用了 PCM 的表达式,但仍然收到此错误:System.Web.Services.Protocols.SoapException: Server was unable to process request。 ---> System.InvalidOperationException:XPath 表达式评估为意外类型 System.Xml.Linq.XAttribute。在 System.Xml.XPath.XPathEvaluator.d__0`1.MoveNext()
  • 您正在调用一个方法,其documentation 表示它选择并返回一个元素,并将一个选择属性的XPath 表达式交给它。尝试寻找其他方法? (也许 XPathEvaluate 会满足您的需要?也许首先使用 XPathSelectElement 选择货币元素,然后在调用 XPathEvaluate 时使用该元素作为上下文节点计算表达式“./@rate”?)。无论如何,请阅读错误消息,就好像它们试图告诉您一些事情一样。他们是。
  • 好吗?为什么要贴出好建议和居高临下的废话?我评论了一个工作单线。伙计,没有积分。
  • 对不起,冒犯了您;没有居高临下的意思。当您报告的错误消息清楚地表明问题的性质并且从问题的性质得出明显的解决方案时,您现在已经两次询问如何解决问题。在我看来,好像您没有仔细查看错误消息;如果你想从别人那里得到帮助,你希望看起来好像你正在付出一些努力。
猜你喜欢
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 1970-01-01
  • 2017-07-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多