Mj1*_*992 5 xml isolatedstorage windows-phone-7
我是WP7的新手.我按照本教程读取和编写了一个xml文件,但是当我读取xml文件时,它只显示xml文件的顶行.我不知道如何检查天气xml文件是由程序正确编写的.所以.
1.在哪里检查保存在独立存储中的xml文件.
2.如何摆脱这个问题.
我在独立存储中编写Xml文件的代码:
using (IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream("mz1.xml", FileMode.Create, myIsolatedStorage))
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (XmlWriter writer = XmlWriter.Create(isoStream, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("person");
writer.WriteElementString("node1", "value1");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
}
}
}
Run Code Online (Sandbox Code Playgroud)
从隔离存储读取Xml文件的代码:
using (IsolatedStorageFile myIsolatedStorage =
IsolatedStorageFile.GetUserStoreForApplication())
{
IsolatedStorageFileStream isoFileStream =
myIsolatedStorage.OpenFile("mz1.xml", FileMode.Open);
using (StreamReader reader = new StreamReader(isoFileStream))
{
textBlock1.Text= reader.ReadToEnd();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
<?xml version="1.0" encoding="utf-8"?>
Run Code Online (Sandbox Code Playgroud)
回答您的第一个问题,您可以从codeplex下载并安装WP7 Isolated Storage Explorer:http://wp7explorer.codeplex.com/
它真的很容易使用.只需在app.xaml.cs中添加几行代码,就可以了.
关于你的第二个问题,你在那里的代码看起来没问题.我最近写了一个小WP7应用程序也做了这种事情.以下是一些代码:
public List<Task> GetTasks()
{
var tasks = new List<Task>();
using (IsolatedStorageFile store = IsolatedStorageFile.GetUserStoreForApplication())
{
if (store.FileExists(XmlFile))
{
//store.DeleteFile(XmlFile);
//XDocument doc = XDocument.Load(store.OpenFile(XmlFile, FileMode.Open));
using (var sr = new StreamReader(new IsolatedStorageFileStream(XmlFile, FileMode.Open, store)))
{
XDocument doc = XDocument.Load(sr);
tasks = (from d in doc.Descendants("task")
select new Task
{
Category = (string) d.Attribute("category"),
Id = (string) d.Attribute("id"),
Name = (string) d.Element("name"),
CreateDate = (DateTime) d.Element("createdate"),
DueDate = (DateTime) d.Element("duedate"),
IsComplete = (bool) d.Element("isComplete")
}).ToList<Task>();
}
}
}
return tasks;
}
Run Code Online (Sandbox Code Playgroud)
它取决于你,但你可能想考虑使用LinqToXml.它使事情有点清洁恕我直言.
我实际上有一篇博文,其中所有这些都发布在这里:
http://www.ritzcovan.com/2012/02/building-a-simple-windows-phone-apppart-2/
你也可以下载所有的代码.我希望你觉得这对你有帮助.
| 归档时间: |
|
| 查看次数: |
3249 次 |
| 最近记录: |