Skip to content

Instantly share code, notes, and snippets.

@y-gagar1n
Created May 27, 2013 10:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save y-gagar1n/5656467 to your computer and use it in GitHub Desktop.
Save y-gagar1n/5656467 to your computer and use it in GitHub Desktop.
C# - Get XML from remote API
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