Ruby Kousinovali
Ruby Kousinovali

Reputation: 347

Read XML from URL and bind to Listbox in WP7

I have a WP7 which read a XML file, take some elements and bind them to a 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 on a URL.

Can someone help me?

Thank you

Upvotes: 3

Views: 2143

Answers (1)

MarcinJuraszek
MarcinJuraszek

Reputation: 125650

You should use WebClient class to get the content from URL and then parse it to XDocument object:

You can try something like that:

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

and the 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
    }
}

Upvotes: 3

Related Questions