您好我正在尝试使用ISO存储从xml文件中删除元素,节点被删除但xml会变形。
代码是:
public void deleteFromXML(string appName)
{
using (IsolatedStorageFile isoStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = isoStorage.OpenFile("ISOconfig.xml", FileMode.Open, FileAccess.ReadWrite))
{
XDocument xml = XDocument.Load(stream);
xml.Descendants("ContainerApp")
.Where(x => x.Element("AppName").Value.Equals(appName))
.Remove();
// stream.Position = 0;
// xml.Save(stream);
stream.Close();
// Save To ISOconfig.xml File oeration not permited on IsolatedStorageFileStream
using (IsolatedStorageFileStream newStream = new IsolatedStorageFileStream("ISOconfig.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, isoStorage))
{
xml.Save(newStream);
}
}
}
}
答案 0 :(得分:0)
所做的更改是在linq查询关闭流并将其保存为:
之后stream.Close(); //保存到ISOconfig.xml文件 using(IsolatedStorageFileStream newStream = new IsolatedStorageFileStream(“ISOconfig.xml”,FileMode.Create,isoStorage)) { xml.Save(方通); }