XDocument.save()没有';不起作用
本文关键字:不起作用 没有 save XDocument | 更新日期: 2023-09-27 18:27:22
我正在使用visual studio 2015为windows桌面制作一个应用程序。我试图打开insert元素并保存文档,但当我使用函数.Save()
时,它不会保存文件
这是我的代码
String filePath = "person.xml";
if (File.Exists(filePath))
{
XDocument collection = XDocument.Load(filePath);
XElement person = new XElement("user",
new XElement("id", this.getId()),
new XElement("name", this.getName()),
new XElement("info", this.getInfo()));
collection.Element("root").Add(person);
using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = file.OpenFile(filePath, FileMode.OpenOrCreate))
{
collection.Save(stream);
}
}
}
else
{
XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes");
XDocument collection = new XDocument(declaration);
XElement person = new XElement("root",
new XElement("user",
new XElement("id", this.getId()),
new XElement("name", this.getName()),
new XElement("info", this.getInfo())));
collection.Add(person);
using (var file = IsolatedStorageFile.GetUserStoreForApplication())
{
using (var stream = file.CreateFile(filePath))
{
collection.Save(stream);
}
}
}
但是当我尝试加载文件时,它会出错,文件不存在
编辑:加载xml:的代码
XDocument collection = XDocument.Load("person.xml");
foreach(XElement a in collection.Descendants("user")) {
Person temp = new Person((int)a.Element("id"), (string)a.Element("info"), (string)a.Element("name"));
lPerson.Add(temp);
lPerson.nbPerson++;
}
您不需要使用文件流。
只是做一个收集。保存(文件名);
编辑:您是否打算尝试将person.xml文件写入collections.xml?您正试图以collection.xml的形式打开,但我看不出您是如何保存的。
如果你只是写回文件,那么使用这个:
collection.Element("root").Add(person);
collection.Save(filepath)'
编辑#2:如果您试图向独立存储进行写入,则需要采取不同的做法。这是我找到的关于如何做到这一点的文件。
https://msdn.microsoft.com/en-us/library/cc189085%28v=vs.95%29.aspx
除非显式创建存储应用程序的文件"person.xml",否则第一个if语句将始终为false。你想要的是我在下面粘贴的东西。
string fileName = "person.xml";
using (IsolatedStorageFile isoManager = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoManager.FileExists(fileName))
{
XDocument collection = XDocument.Load(fileName);
XElement person = new XElement("user",
new XElement("id", "someid"),
new XElement("name", "someName"),
new XElement("info", "someInfo"));
collection.Element("root").Add(person);
using (IsolatedStorageFileStream fileWriter = isoManager.OpenFile(fileName, FileMode.Open))
{
using (XmlWriter writer = XmlWriter.Create(fileWriter))
{
collection.WriteTo(writer);
}
}
}
else
{
XDeclaration declaration = new XDeclaration("1.0", "utf-8", "yes");
XDocument collection = new XDocument(declaration);
XElement person = new XElement("root",
new XElement("user",
new XElement("id", "someid"),
new XElement("name", "someName"),
new XElement("info", "someInfo")));
collection.Add(person);
using (IsolatedStorageFileStream fileWriter = isoManager.CreateFile(fileName))
{
using (XmlWriter writer = XmlWriter.Create(fileWriter))
{
collection.WriteTo(writer);
}
}
}
}
EDIT:除非您创建了存储应用程序的文件"collections.xml",否则您的读取方法永远不会工作,请使用以下内容:
string fileName = "collections.xml";
using (IsolatedStorageFile isoManager = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isoManager.FileExists(fileName))
{
XDocument document;
using (IsolatedStorageFileStream reader = isoManager.OpenFile(fileName, FileMode.Open))
{
document = XDocument.Load(reader);
}
foreach (XElement a in document.Descendants("user"))
{
Person temp = new Person((int)a.Element("id"), (string)a.Element("info"), (string)a.Element("name"));
lPerson.Add(temp);
lPerson.nbPerson++;
}
}
}