0
votes

Apologies if this is a bit of a simple one, but I'm quite new to C#. In a WP7 app, I'm trying to load an XML file (specifically, a Blogger feed) into an XDocument using the XDocument.Load() method. However, when I try the following:

XDocument data = XDocument.Load("http://destroyedordamaged.blogspot.com/feeds/posts/default");

I get the error:

Cannot open 'http://destroyedordamaged.blogspot.com/feeds/posts/default'. The Uri parameter must be a relative path pointing to content inside the Silverlight application's XAP package. If you need to load content from an arbitrary Uri, please see the documentation on Loading XML content using WebClient/HttpWebRequest

So I had a look around, and found someone who suggested that I do this instead:

WebClient wc = new WebClient();
wc.OpenReadCompleted += wc_OpenReadCompleted;
wc.OpenReadAsync(new Uri("http://destroyedordamaged.blogspot.com/feeds/posts/default"));

and:

private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            Console.WriteLine("THERE IS AN ERROR: "+e.Error.Message);
            return;
        }
        using (Stream s = e.Result)
        {
            data = XDocument.Load(s);
        }
    } 

But this doesn't seem to work either; it loads nothing into the XDocument. Is there something I'm missing here? I'd like to find out the simplest way to load the xml from the feed into an XDocument.

I had a look around, but it seems like everyone who has had a problem like this has been pointing their code at a specific .xml file rather than a URL without an extension like mine.

I'd appreciate any input you can provide. Thanks a lot in advance.

2

2 Answers

2
votes

Try using DownloadStringAsync instead of OpenReadAsync:

var webClient = new WebClient();
webClient.DownloadStringCompleted += RequestCompleted;
webClient.DownloadStringAsync(new Uri(SOURCE));

And the RequestCompleted code:

    private void RequestCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            var feedXml = XDocument.Parse(e.Result);
            // (...)
        }
    }

I used it in my app some time ago, so I'm pretty sure it works.

0
votes

Check out the Weather Forecast Sample the Windows Phone Code Samples. I used this sample to get familiar with how to do the things. Download the sourcecode and check out Forecast.cs.

Sample code link