使用 Web 客户端下载的字符串构建 XDocument
我正在使用以下方法下载 xml 文件,
private void LoadXMLFile()
{
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
xmlClient.DownloadStringAsync(new Uri("chart.xml", UriKind.RelativeOrAbsolute));
}
void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
x2 = new XDocument(xmlData);
}
}
我想使用 xmlData 中的信息来构建 xDocument,就像我在最后一行中尝试做的那样。它没有给出任何错误,但我的程序无法工作,所以我一定没有正确制作 xDocument。像这样将 xml 文档直接分配给 x2
x2 = Xdocument.Load("chart.xml")
是有效的。
但我需要通过网络客户端来完成。我在这里做错了什么
I am using the following methods to download an xml file
private void LoadXMLFile()
{
WebClient xmlClient = new WebClient();
xmlClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(XMLFileLoaded);
xmlClient.DownloadStringAsync(new Uri("chart.xml", UriKind.RelativeOrAbsolute));
}
void XMLFileLoaded(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error == null)
{
string xmlData = e.Result;
HtmlPage.Window.Alert(xmlData);
x2 = new XDocument(xmlData);
}
}
I want to use the information inside xmlData to build an xDocument, like I am trying to do in my last line. It does not give any errors but my program does not work so I must not be correctly making the xDocument. Assiging an xml document directly to x2 like this
x2 = Xdocument.Load("chart.xml")
works.
But I need to do it through webclient. what am I doing wrong here
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
获得
xmlData
字符串后,就很容易使用XDocument.Parse
:您能否详细说明为什么需要使用
WebClient
而不是XDocument.Load< /code> 不过?是为了让调用异步吗?
Once you've got the
xmlData
string, it's easy - useXDocument.Parse
:Could you elaborate why you need to use
WebClient
rather thanXDocument.Load
though? Is it to make the call asynchronous?XDocument.Parse
XDocument.Parse