我有一个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上。
有人可以帮助我吗?
谢谢
答案 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
}
}