【发布时间】:2014-03-13 22:39:52
【问题描述】:
当我第二次添加 xml 文件时返回错误如何解决这个问题 如果文件退出代码运行并添加一个 xml 元素,并且当我尝试添加第三次在我的图像文件中返回错误错误帮助我,则第一次运行 else 代码以在隔离存储中添加文件第二次。 这是我的代码
private void AddXml(List<Bookmarkdata> data ,string filename, string fileid,string fullpath,string currentpage)
{
XDocument doc = null;
TextWriter writer = null;
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (myIsolatedStorage.FileExists("BookmarkFile.xml"))
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("BookmarkFile.xml", System.IO.FileMode.Open, FileAccess.ReadWrite))
{
XDocument _doc = XDocument.Load(stream);
XElement _item = new XElement("Bookmarkdata");
XAttribute Bookname = new XAttribute("Bookname", filename);
XAttribute Bookid = new XAttribute("Bookid", fileid);
XAttribute BookPath = new XAttribute("BookPath", fullpath);
XAttribute Pageno = new XAttribute("Pageno", currentpage);
_item.Add(Bookname, Bookid, BookPath, Pageno);
_doc.Root.Add(new XElement(_item));
_doc.Save(stream);
}
}
else
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile("BookmarkFile.xml", FileMode.Create))
{
writer = new StreamWriter(stream);
XmlSerializer serializer = new XmlSerializer(typeof(List<Bookmarkdata>));
// using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
// {
serializer.Serialize(writer, data);
// }
}
}
}
【问题讨论】:
标签: xml c#-4.0 windows-phone-8-emulator