我在使用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
答案 0 :(得分:0)
这有效:
<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;
答案 1 :(得分:0)
您的错误消息试图告诉您XPath有语法错误。对于
"exhangerates/dailyrates/currency[@code='"+ country +"']@rate"
读
"exhangerates/dailyrates/currency[@code='"+ country +"']/@rate"
也就是说,在导航到currency
节点的步骤和导航到其rate
属性的后续步骤之间插入斜杠。 (我看到PCM打败了我这个解决方案。帽子。)