刚才的发错呢。
我用的是将数据保存在文件中:
using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Xml;
using System.Collections.Generic;
using System.Xml.Serialization;
namespace NoteText
{
public class XmlMessage
{
//using System.Collections.Generic 对应list
//将对象写入xml文档中
public void WriteMessage(List<Item> itemslist)
{
var isoFile = IsolatedStorageFile.GetUserStoreForApplication();
string filePath = System.IO.Path.Combine("note", "note.xml");
if (!isoFile.DirectoryExists("note"))
{
isoFile.CreateDirectory("note");
}
FileStream fs = isoFile.CreateFile(filePath);
fs.Close();
XmlWriterSettings xmlWriterSetting = new XmlWriterSettings();
xmlWriterSetting.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile(filePath, FileMode.Create))
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(List<Item>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSetting))
{
xmlSerializer.Serialize(xmlWriter, itemslist);
}
}
}
}
// //读取xml文档的内容
public List<Item> ReadMessage()
{
List<Item> itemlist;
using (IsolatedStorageFile myIsolatedStorageFile=IsolatedStorageFile.GetUserStoreForApplication())
{
string filePath = System.IO.Path.Combine("note", "note.xml");
using (IsolatedStorageFileStream stream =myIsolatedStorageFile.OpenFile (filePath,FileMode .Open ))
{
XmlSerializer serializer = new XmlSerializer(typeof (List<Item>));
itemlist = (List<Item>)serializer.Deserialize(stream);
}
}
return itemlist;
}
}
}
Item是自己定义的类
不知道对你有用处吗? |