0
XDocument没有与XMLDocument相反的加载方法,因此如何使用url从Internet加载XML内容?如何在Silverlight中的XDocument中加载xml文件?
XDocument没有与XMLDocument相反的加载方法,因此如何使用url从Internet加载XML内容?如何在Silverlight中的XDocument中加载xml文件?
实际上,XDocument
确实有一个Load(Uri)
方法,但它仅用于导航到您的应用程序中的页面。这是一个静态的方法,所以你做XDocument xDoc = XDocument.Load("/somepage.xaml");
。 Load(string)
方法的文档是here。
如果要访问外部URL,则需要使用WebClient
类。下面是我在Windows Phone 7应用测试的例子(这基本上是SL3):
using System;
using System.Net;
using Microsoft.Phone.Controls;
using System.Xml.Linq;
namespace XDocumentTest
{
public partial class MainPage : PhoneApplicationPage
{
// Constructor
public MainPage()
{
InitializeComponent();
WebClient wc = new WebClient();
wc.DownloadStringCompleted += HttpsCompleted;
wc.DownloadStringAsync(new Uri("http://twitter.com/statuses/public_timeline.xml"));
}
private void HttpsCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
XDocument xdoc = XDocument.Parse(e.Result, LoadOptions.None);
TextBlock1.Text = xdoc.FirstNode.ToString();
}
}
}
}
This question是类似的,但涉及https
,我不认为你处理。
非常感谢! – user310291 2011-03-18 05:25:40
你好,我试过,但它不会调用处理程序http://stackoverflow.com/questions/5423591/why-httpscompleted-is-not-called – user310291 2011-03-24 19:34:22