如何加载一个XML文件,并在Silverlight中读取它? [英] How do I load an xml file and read from it in Silverlight?

查看:116
本文介绍了如何加载一个XML文件,并在Silverlight中读取它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不想从一个网站我已经把它添加到我的项目加载它所有我想要做的就是访问它,阅读它。 !谁能帮

I do not want to load it from a website I have added it to my project all I want to do is access it and read it. Can anyone help!

推荐答案

我发现通过的迈克·斯诺的描述的一种方法。该代码是有点长完全引用,但这里的相关部分:

I've found a blog post by Mike Snow which describes one method. The code's a bit long to quote in full, but here's the relevant section:

StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);

while (reader.Read())
{
    // Do stuff
}

XML文件使用此代码下载:

The xml file is downloading using this code:

Uri url = new Uri("MapImages.xml", UriKind.Relative);
WebClient client = new WebClient();
client.DownloadStringCompleted +=
        new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);

从的 DownloadStringAsync MSDN页:

From the DownloadStringAsync MSDN page:

下载资源后,该方法使用Encoding属性指定的编码,以资源转换为字符串。在下载资源此方法不会阻止调用线程。要下载的资源和块在等待服务器的响应,使用DownloadString方法。当下载完成后,DownloadStringCompleted事件引发。应用程序必须处理此事件以接收通知。下载的字符串是在Result属性中可用。

After downloading the resource, this method uses the encoding specified in the Encoding property to convert the resource to a String. This method does not block the calling thread while downloading the resource. To download a resource and block while waiting for the server's response, use the DownloadString method. When the download completes, the DownloadStringCompleted event is raised. Your application must handle this event to receive notification. The downloaded string is available in the Result property.

因此,该文件下载到您的Internet临时文件夹(或高速缓存依赖于浏览器),然后将该文件的传递一个字符串以事件处理程序,在那里你可以使用 StringReader 读它。

So it downloads the file to your temporary internet files folder (or cache depending on browser) and then passes the file as a string to the event handler, where you can read it using StringReader.

这篇关于如何加载一个XML文件,并在Silverlight中读取它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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