我使用xignite API获取实时货币交换数据。当我使用我的查询字符串:在C#中使用XDoc获取XML元素的内容
http://globalcurrencies.xignite.com/xGlobalCurrencies.xml/GetRealTimeRate?Symbol=GBPEUR&_token=[mytoken]
我得到如下:
<Rate xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.xignite.com/services/">
<Outcome>Success</Outcome>
<Identity>Request</Identity>
<Delay>0.0218855</Delay>
<BaseCurrency>USD</BaseCurrency>
<QuoteCurrency>EUR</QuoteCurrency>
<Symbol>USDEUR</Symbol>
<Date>08/24/2016</Date>
<Time>3:23:34 PM</Time>
<QuoteType>Calculated</QuoteType>
<Bid>0.889126</Bid>
<Mid>0.88915</Mid>
<Ask>0.889173</Ask>
<Spread>4.74352E-05</Spread>
<Text>
1 United States dollar = 0.88915 European Union euro
</Text>
<Source>Rate calculated from EUR:USD</Source>
</Rate>
我试图访问Mid
元素的内容,到目前为止,我这样做
var xDoc = XDocument.Load(
"http://globalcurrencies.xignite.com/xGlobalCurrencies.xml/GetRealTimeRate?Symbol="
+ "GBP" + "EUR" + "&_token=[MyToken]");
string s = (string)xDoc.Root.Element("Mid");
output.Text = s;
该xDoc
变量返回与我以前显示的XML,但当我尝试获取Mid
元素的内容时,string s
是null
。如何使用XDoc访问元素Mid
的内容?
我想这一点,但我有与 NS,REG和MyObject来 他们有什么麻烦吗? – user3711612
问题中的代码也使用LINQ to XML,但不起作用。我不确定通用示例将如何帮助发现当前代码中的错误。 –
@ user3711612我之前的代码存在错误,我修正了它... MyObject在我的示例中将是您的Rate对象,您可以直接从xml中提取并实例化速率对象,然后访问Rate对象中的Mid属性 – miguelmpn