![](/img/trans.png)
[英]In gridview Datakey field contains double value but retutns only integer part,how to get complete double value
[英]How to get integer/double value from XML
我正在嘗試使用來自Yahoo的YQL獲取股票數據。
我嘗試使用XML來完成此操作,但是在獲取整數值時遇到了麻煩(我認為我需要使用double,因為價格以十進制表示)。 最初,我可以獲取字符串值,例如“ Currency”,但是我更改了一些代碼,無法再返回任何內容。
我試圖從節點獲取值以顯示在文本框中(tbValue);
string url = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&env=store://datatables.org/alltableswithkeys";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url)
XmlNode field = xmlDoc.SelectSingleNode("/query/results/quote/Name");
string desiredValue = "";
if (field != null ) desiredValue = field.Value;
MessageBox.Show(desiredValue);
tbValue.Text = ptest;
在嘗試獲取雙節點時,我嘗試使用int.Parse(“ string”)...但是我無法使其正常工作。
任何幫助,將不勝感激,謝謝。
我不確定您的問題是什么,但是此Linq2Xml代碼返回正確的Bid
值(例如, 將節點強制轉換為正確的類型 )
var xDoc = XDocument.Load(url);
var bid = (decimal)xDoc.XPathSelectElement("/query/results/quote/Bid");
PS:您將需要
using System.Xml.XPath;
using System.Xml.Linq;
您應該使用field.InnerText
, field.Value
在你的情況下返回null。
這段代碼在這里工作。
static void Main(string[] args)
{
//Error Trapping
string url = @"http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20(%22AAPL%22)&env=store://datatables.org/alltableswithkeys";
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(url);
XmlNode field = xmlDoc.SelectSingleNode("/query/results/quote/Name");
string desiredValue = "";
if (field != null )
desiredValue = field.InnerText;
Console.WriteLine(desiredValue);
}
如果您想記多個筆記。
XmlNodeList nodes = xmlDoc.SelectNodes("/query/results/quote/Name");
foreach(XmlNode node in nodes)
{
if (node != null)
Console.WriteLine(node.InnerText);
}
如果值是整數/小數,則可以從字符串轉換為它們!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.