This is the link for my xml data
http://api.worldbank.org/countries/IND/indicators/EN.ATM.CO2E.PC?per_page=10&date=2005:2012
i am using the following code to parse it and display but unable to do so.
public void Getinfo()
{
try
{
String url = http://api.worldbank.org/countries/IND/indicators/EN.ATM.CO2E.PC?per_page=10&date=2005:2012";
WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(new Uri(url));
}
catch (Exception)
{
MessageBox.Show("Please retry unable to access Data");
}
}
private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
if (e.Error != null)
{
MessageBox.Show(e.Error + "");
return;
}
using (Stream s = e.Result)
{
XDocument doc = XDocument.Load(s);
XNamespace wb = "http://www.worldbank.org";
foreach (var node in doc.Element(wb+"data").Element(wb+"data").Elements(wb+"date"))
{
String chk = node.Value.ToString();
String year1 = "2007";
if (chk == year1)
{
foreach (var node1 in doc.Element(wb+"data").Element(wb+"data").Elements(wb+"value"))
{
info1.Text = node1.Value.ToString();
}
}
The problem comes from your Linq request:
What you are doing here:
You take the first data element, then you take the first data element again in the previous, and then you take all the date in that second data element.
What you want to do is:
Take all the data element at the second level, but don’t take the whole element, just the first date element of each data element.
What you want to achieve isn’t clear for me, but then if you need the value element in the same data element, maybe it isn’t necessary to return only the date.
Also, why do you use a second foreach statement? Once again, you’ll only receive one element with that request. I guess what you want to do is something like this: