1
votes

I'm trying to parse the RSS feed from http://b.fanfiction.net/atom/l/224/3/0/1/1/50/0/0/0/0 but can't search by element name for some reason. I'm following the instructions from this page but his example doesn't seem to work on the fanfiction site above.

Here's the code that I'm using:

private void getData_Click(object sender, RoutedEventArgs e)
{
    String rss = "http://b.fanfiction.net/atom/l/224/3/0/1/1/50/0/0/0/0";

    HttpWebRequest request = HttpWebRequest.CreateHttp(rss);
    request.BeginGetResponse(
        asyncCallback =>
        {
            string data = null;

            using (WebResponse response = request.EndGetResponse(asyncCallback))
            {
                using (StreamReader reader = new StreamReader(response.GetResponseStream()))
                {
                    data = reader.ReadToEnd();
                }
            }
            Deployment.Current.Dispatcher.BeginInvoke(() => HttpsCompleted(data));
        }
        , null);
}

private void HttpsCompleted(string feedString)
{
    // build XML DOM from feed string
    XDocument doc = XDocument.Parse(feedString);

    // show title of feed in TextBlock
    XElement feed = doc.Element("feed");
    XElement title = feed.Element("title");
    String txt_title = title.Value;
    txtBlk_FeedDescription.Text = txt_title; 
    // add each feed item to a ListBox
    foreach (var item in doc.Descendants("entry"))
    {
        listBox1.Items.Add(item.Element("title").Value);
    }
}

I tried just listing all the elements and sub elements of doc by running:

  foreach (XElement element in doc.Elements())
        {
            txtBlk_FeedDescription.Text += element.Name.ToString() + "\n";
            foreach (XElement subelement in element.Elements())
            {
                txtBlk_FeedDescription.Text += subelement.Name.ToString() + "\n";
            }
        }

And that resulted in the following list:

{http://www.w3.org/2005/Atom}feed

{http://www.w3.org/2005/Atom}author

{http://www.w3.org/2005/Atom}title

{http://www.w3.org/2005/Atom}subtitle

{http://www.w3.org/2005/Atom}link

{http://www.w3.org/2005/Atom}updated

{http://www.w3.org/2005/Atom}id

{http://www.w3.org/2005/Atom}entry

{http://www.w3.org/2005/Atom}entry

{http://www.w3.org/2005/Atom}entry

...

Any help on what I'm supposed to do here?

Thanks!

1

1 Answers

3
votes

Your Linq to XML query is looking for elements by name, however you are not including the namespace. If you inspect the source document you will see that it has teh following namespace:

xmlns="http://www.w3.org/2005/Atom"

See the MSDN document on working with XML namespaces. For example, the following will select the feed element:

XNamespace ns = "http://www.w3.org/2005/Atom";

XElement feed = doc.Element(ns + "feed");

You need to add the namespace to all your element names.