我正在尝试在IsolatedStorage中创建一个.xml文件并更新它但是当我尝试读取该文件时,我得到了XMLException。这是我的代码
using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication())
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
try
{
IsolatedStorageFileStream isoFileStream = isoStore.OpenFile("Favourites.xml", FileMode.Open, FileAccess.ReadWrite);
using (XmlWriter writer = XmlWriter.Create(isoFileStream, settings))
{
XDocument doc = XDocument.Load(isoFileStream);
doc.Root.Add(
new XElement("recipe",
new XAttribute("id", thisRecipe.RecipeId),
new XAttribute("title", thisRecipe.Title),
new XAttribute("youtubeid", thisRecipe.YoutubeId)));
doc.WriteTo(writer);
writer.Flush();
}
}
catch
{
using (IsolatedStorageFileStream isoFileStream =
isoStore.OpenFile("Favourites.xml", FileMode.Create, FileAccess.ReadWrite))
{
using (XmlWriter writer = XmlWriter.Create(isoFileStream, settings))
{
writer.WriteStartDocument();
writer.WriteStartElement("recipes");
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
writer.Close();
}
}
using (IsolatedStorageFileStream isoFileStream =
isoStore.OpenFile("Favourites.xml", FileMode.Open, FileAccess.ReadWrite))
{
using (XmlWriter writer = XmlWriter.Create(isoFileStream, settings))
{
XDocument doc = XDocument.Load(isoFileStream);
doc.Root.Add(
new XElement("recipe",
new XAttribute("id", thisRecipe.RecipeId),
new XAttribute("title", thisRecipe.Title),
new XAttribute("youtubeid", thisRecipe.YoutubeId)));
doc.WriteTo(writer);
writer.Flush();
}
}
}
}
这是我尝试创建然后更新的类型xml文件:
<?xml version="1.0" encoding="utf-8"?>
<recipes>
<recipe id="1" title="dessert" youtubeid="eQ1qos__ZrA" />
<recipe id="2" title="starter" youtubeid="5t2uqOkc4NQ"/>
<recipe id="3" title="beverages" youtubeid="PqJsUibxTIk"/>
</recipes>