我不想从网站加载它,我已将其添加到我的项目中,我想做的就是访问它并阅读它。谁能帮忙!
最佳答案
我发现了 Mike Snow 的博客文章其中描述了一种方法。完整引用该代码有点长,但以下是相关部分:
StringReader stream = new StringReader(e.Result);
XmlReader reader = XmlReader.Create(stream);
while (reader.Read())
{
// Do stuff
}
使用以下代码下载 xml 文件:
Uri url = new Uri("MapImages.xml", UriKind.Relative);
WebClient client = new WebClient();
client.DownloadStringCompleted +=
new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.DownloadStringAsync(url);
来自DownloadStringAsync
MSDN 页面:
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 文件文件夹(或缓存,具体取决于浏览器),然后将文件作为字符串传递到事件处理程序,您可以在其中使用 读取它字符串读取器
。
关于c# - 如何在 Silverlight 中加载 xml 文件并从中读取?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3038711/