Search code examples
c#linq-to-xmlfeedparser

Getting specific Xml node


How can I get "entry" nodes from this feed

http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348

I tried linq to xml but I think because of the existing attributes of entry tags following code does not work.

string url = "http://www.google.com/alerts/feeds/14392773026536511983/5526937985735563348";

WebClient c = new WebClient();

string xml = ASCIIEncoding.Default.GetString(c.DownloadData(url));

XDocument doc = XDocument.Parse(xml);

var entries = doc.Descendants("entry");

Thanks in advance,


Solution

  • You're not specifying a namespace. Try this:

    XNamespace atom = "http://www.w3.org/2005/Atom";
    var entries = doc.Descendants(atom + "entry");
    

    Btw, I wouldn't use ASCII for this, or DownloadData... use WebClient.DownloadString to handle the encoding for you. Or indeed, just use XDocument.Load(url):

    For example:

    string url = ...;
    
    XDocument doc = XDocument.Load(url);
    XNamespace atom = "http://www.w3.org/2005/Atom";
    var entries = doc.Descendants(atom + "entry");
    Console.WriteLine(entries.Count()); // Prints 20