2

I have an XML file that I want to access in an Windows Phone 7 and Silverlight application. Th XML file is on a webserver, and I want to access it through http://www.mydomain.com/data/this_is_my_file.xml.

How do I use this URL to load the XML file into an XDocument?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Filip
  • 1,098
  • 5
  • 17
  • 34

1 Answers1

11

You can use WebClient or HttpWebRequest to download (asynchronously) and parse the response. One of the simplest approach to download and parse XML from the web is below -

public void LoadXmlItems(string xmlUrl)
{
   WebClient client = new WebClient();
   
   client.OpenReadCompleted += (sender, e) =>
   {
        if (e.Error != null)
            return;

        Stream str = e.Result;
        XDocument xdoc = XDocument.Load(str);

        // take 10 first results
        List<RssFeedItem> rssFeedItems = (from item in xdoc.Descendants("item")
                                            select new XmlItem()
                                            {
                                                Title = item.Element("title").Value,
                                                Description = item.Element("description").Value,
                                                Url = new Uri(item.Element("link").Value, UriKind.Absolute)
                                            }).ToList();
        // close
        str.Close();

        // add results to the list
        XmlItems.Clear();
        foreach (RssFeedItem item in rssFeedItems)
        {
           XmlItems.Add(item);
        }
    };
    client.OpenReadAsync(new Uri(xmlUrl, UriKind.Absolute));
}

xmlUrl is the path to the XML file on the web. XmlItem is a class like so -

public class XmlItem
{
  public string Title { get; set; }
  public string Description { get; set; }
  public Uri Url { get; set; }
}

You need to note that you may encounter cross-thread exception if you are updating an observable collection. In the above example, XmlItems is a List<XmlItem>. However, if you wish to add the XMLItem's to an observable collection, use this piece of code instead -

Dispatcher.BeginInvoke(() =>
{
  XmlItems.Clear();
  foreach (RssFeedItem item in rssFeedItems)
  {
     XmlItems.Add(item);
  }
});

An alternative approach is to use HttpWebRequest. You can read about this approach here and use the code in the sample.

HTH, indyfromoz

Glorfindel
  • 21,988
  • 13
  • 81
  • 109
indyfromoz
  • 4,045
  • 26
  • 24
  • Hi Filip, Indy's demo code looks pretty good. I'd be inclined to use HttpWebRequest rather than WebClient if your download is anything but trivial to avoid negative impacts on your UI's performance. If your download updates the UI directly you can use the dispatcher as shown above to get around cross thread access issues, or pull the data down into a non UI container first, then load this to your ViewModel or other UI bound container via the dispatcher or back on the UI thread. – Mick N Oct 23 '10 at 12:47
  • @MickN - trying to understand your comment..... So, you think I've missed something in my answer? – indyfromoz Oct 23 '10 at 18:46
  • Nah, it's good bud, hence adding to your post rather than writing seperately, just some extra info for Filip to consider. How much it matters for Filip will depend on what he's using it for. – Mick N Oct 24 '10 at 00:35
  • Thats great to hear, I hope my answer helped you – indyfromoz Oct 25 '10 at 10:27