【问题标题】:Read XML from URL and bind to Listbox in WP7从 URL 读取 XML 并绑定到 WP7 中的 Listbox
【发布时间】:2012-03-19 10:24:39
【问题描述】:

我有一个 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 上。

有人可以帮我吗?

谢谢

【问题讨论】:

    标签: xml windows-phone-7


    【解决方案1】:

    您应该使用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
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-04-28
      • 2011-08-14
      • 2011-06-05
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2023-04-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多