从URL读取XML并绑定到WP7中的Listbox

时间:2012-03-18 18:30:55

标签: xml windows-phone-7

我有一个WP7读取 XML文件,取一些元素并将它们绑定到listbox 这是代码:

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; }

现在我想做同样的事情,但XML文件在URL上。

有人可以帮助我吗?

谢谢

1 个答案:

答案 0 :(得分:3)

您应该使用WebClient类从URL获取内容,然后将其解析为XDocument对象:

你可以尝试这样的事情:

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

和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
    }
}