Created
May 27, 2013 10:53
-
-
Save y-gagar1n/5656467 to your computer and use it in GitHub Desktop.
C# - Get XML from remote API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
static IEnumerable<string> FetchStockQuotes(string[] symbols) | |
{ | |
var quotes = new List<string>(); | |
string url = string.Format("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.quotes%20where%20symbol%20in%20({0})&env=store://datatables.org/alltableswithkeys", | |
String.Join("%2C", symbols.Select(s => "%22" + s + "%22"))); | |
var wc = new WebClient {Proxy = WebRequest.DefaultWebProxy}; | |
var ms = new MemoryStream(wc.DownloadData(url)); | |
var reader = new XmlTextReader(ms); | |
XDocument doc = XDocument.Load(reader); | |
XElement results = doc.Root.Element("results"); | |
foreach (string symbol in symbols) | |
{ | |
XElement q = results.Elements("quote").First(w => w.Attribute("symbol").Value == symbol); | |
quotes.Add(symbol + ":" + q.Element("AskRealtime").Value); | |
} | |
return quotes; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment