[c#-4.0] Xml Parsing in C#

http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom on this site, I wrote the following code to retrieve the data.

    protected void Button1_Click(object sender, EventArgs e)     {         string adresal = "http://" + txtAd.Text;         WebResponse GelenCevap;         WebRequest adresistegi = HttpWebRequest.Create(adresal);         GelenCevap = adresistegi.GetResponse();         StreamReader CevapOku = new StreamReader(GelenCevap.GetResponseStream());         string KaynakKodlar = CevapOku.ReadToEnd();          XmlDocument xmlDoc = new XmlDocument(); // Create an XML document object         xmlDoc.Load("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.atom"); // Load the XML document from the specified file           XmlNodeList depremler = xmlDoc.GetElementsByTagName("entry");          foreach (XmlNode node in depremler)         {               var a = node.ChildNodes;             foreach (XmlElement x in a)             {                  ListBox1.Items.Add(x.InnerText);              }          } 

} In this way I get all the data in the ListBox.But I need to assign them to variable data line by line.How can I do? I would appreciate if you can help.

Also i need id,title, updated, georss:point,georss:elev variables.

This question is related to c#-4.0

The answer is


First add an Enrty and Category class:

public class Entry {     public string Id { get; set; }     public string Title { get; set; }     public string Updated { get; set; }     public string Summary { get; set; }     public string GPoint { get; set; }     public string GElev { get; set; }     public List<string> Categories { get; set; } }  public class Category {     public string Label { get; set; }     public string Term { get; set; } } 

Then use LINQ to XML

XDocument xDoc = XDocument.Load("path");          List<Entry> entries = (from x in xDoc.Descendants("entry")             select new Entry()             {                 Id = (string) x.Element("id"),                 Title = (string)x.Element("title"),                 Updated = (string)x.Element("updated"),                 Summary = (string)x.Element("summary"),                 GPoint = (string)x.Element("georss:point"),                 GElev = (string)x.Element("georss:elev"),                 Categories = (from c in x.Elements("category")                                   select new Category                                   {                                       Label = (string)c.Attribute("label"),                                       Term = (string)c.Attribute("term")                                   }).ToList();             }).ToList();