从 URL 读取 XML 并绑定到 WP7 中的列表框 [英] Read XML from URL and bind to Listbox in WP7

查看:21
本文介绍了从 URL 读取 XML 并绑定到 WP7 中的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

Now i want to do the same but the XML file is on a URL.

有人可以帮我吗?

谢谢

推荐答案

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

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

你可以试试这样的:

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

和 HttpCompeted:

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

这篇关于从 URL 读取 XML 并绑定到 WP7 中的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆