在 WP7 中打开本地 xml 文件 [英] Open a local xml file in WP7

查看:26
本文介绍了在 WP7 中打开本地 xml 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个本地 xml 文件,当我的应用程序运行时需要将其加载到独立存储.第一次开始.但如果我使用流或 File.Open 打开该文件,则会引发错误.我还需要以序列化形式存储该文件.请帮助,因为 WP7 的新手和 C# 无法破解它.

Me having a local xml file and need to load it to Isolated Storage when my app. starts for first time. but if i am opening that file using stream OR File.Open it's throwing an error. Also i need to store that file in serialized form. Please help, as being new to WP7 and C# not able to crack it.

以下代码用于打开 XML 文件:

Following code is used for opening the XML file:

FileStream stream = new FileStream("site.xml", FileMode.Open);

推荐答案

如果该文件是您项目的一部分,那么您可以将该文件作为资源添加到您的项目中.这意味着文件的 "Build Action" 应该是 "Resource".下面的函数可以从项目资源中读取文件.

If the file is a part of your project then you can add the file to your project as resource. That means file "Build Action" of the file should be "Resource". And the following function can read the file from project resource.

Stream stream = this.GetType().Assembly.GetManifestResourceStream("Namespace.FileName.FileExtentation");

现在您可以根据文件类型读取流.如果它是一个 XML 文件,那么您可以像下面一样阅读它.

Now you can read the stream according to the file type. If it is a XML file then you can read it like below.

举个例子

XElement element = XElement.Load(stream);
foreach (XElement phraseElement in element.Elements())
{
    MyClass foo = new foo();
    foreach (XAttribute attribute in phraseElement.Attributes())
      foo.Text = attribute.Value;

}

如何写入隔离存储和从隔离存储读取文件?请查看以下链接

http://www.codebadger.com/blog/post/2010/09/03/Using-Isolated-Storage-on-Windows-Phone-7.aspx

您也可以查看以下有关 Windows Phone 本地数据存储的链接.

Also you can checkout the link below regarding Local Data Storage for Windows Phone.

http://msdn.microsoft.com/en-us/library/ff626522%28v=vs.92%29.aspx

这篇关于在 WP7 中打开本地 xml 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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