2014-10-04 3 views
0

저는 Yahoo에서 YQL을 사용하여 주식 데이터를 얻으려고합니다.XML에서 정수/이중 값을 얻는 방법

XML을 사용하여이 작업을 수행하려고했지만 정수 값을 가져 오는 데 문제가 발생했습니다 (가격을 소수로 사용할 필요가 있다고 생각합니다). 원래 '통화'와 같은 문자열 값을 얻을 수 있었지만 일부 코드가 변경되어 더 이상 반환 할 수 없습니다.

노드에서 값을 가져 와서 텍스트 상자 (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")를 사용하려고했으나 작동하지 못했습니다.

감사합니다. 감사드립니다.

답변

1

필요 당신은 귀하의 경우는 null field.InnerText, field.Value 수익을 사용해야합니다.

이 코드는 작동합니다.

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); 
     } 

값이 정수/십진수이면 문자열!

+0

좋습니다. InnerText는 int/decimal 인 경우에도 문자열로 값을 가져 오므로 변환 할 필요가 없습니다. 도움을 주셔서 감사합니다. – adventuresncode

+0

@adventuresncode 당신은 환영합니다, 당신의 프로젝트 또는 과제와 행운을 비네 :) – mybirthname

1

나는 당신의 문제가 무엇인지 잘 모르겠지만,이 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; 
관련 문제