Reading XML from URL and Linking to Listbox in WP7

I have WP7 that reads an XML file , takes some elements and links them to listbox

Here is the code:

XDocument data = XDocument.Load("file.xml");

var persons = from query in data.Descendants("Table")
select new Person
{
Phone = (string)query.Element("Phone"),
Name= (string)query.Element("Name"),
};

listBox1.ItemsSource = persons;

public class Person
{
string Phone;
string Name;

public string Phone
{
 get { return phone; }
 set { phone = value; }
}

public string ame
{
get { return name; }
set { name = value; }

      

Now I want to do the same, but the XML file is in the url.

Can anyone help me?

thank

+3


source to share


1 answer


You have to use a class WebClient

to get the content from the url and then parse it down to the object XDocument

:

You can try something like this:



WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpCompleted;
wc.DownloadStringAsync(new Uri("http://domain/path/file.xml"));

      

and HttpCompeted:

private void HttpCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    if (e.Error == null)
    {
        XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);

        // do something with the XDocument here
    }
}

      

+3


source







All Articles