我正在尝试在 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>