c# - 如何在 Silverlight 中加载 xml 文件并从中读取?

标签 c# xml silverlight

我不想从网站加载它,我已将其添加到我的项目中,我想做的就是访问它并阅读它。谁能帮忙!

最佳答案

我发现了 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/

相关文章:

c# - 如何使用正则表达式匹配引号中的字符串

xml - 如何在 Grails 中动态呈现 XML?

c# - 通过 C# 的动态 XAML

c# - EF - 导航属性为空,即使在重新加载实体之后,但在程序重新启动时有效

c# - 如何在 C# 中生成 http ETag?

Android XML 布局问题

xml - 如何限制 RCP 扩展点中的字符串属性?

c# - 在自己的控制中传递验证错误

.net - 是否有一种简单的方法可以将服务引用添加到一个程序集,但将客户端配置保留在另一个程序集中?

c# - UnitOfWork 模式是否会在 EF 应用程序中增加值(value)?