我使用visual studio 2015制作Windows桌面应用程序。
我尝试打开插入元素并保存文档,但是当我使用函数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);
}
}
}
时,它不会保存文件
这是我的代码
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++;
}
但是当我尝试加载文件时,它会转到错误,文件不存在
编辑: 用于加载xml的代码:
string _instructorNameInput;
public string InstructorNameInput
{
get { return _instructorNameInput; }
set
{
this.RaiseAndSetIfChanged(ref _instructorNameInput, value);
Submit.RaiseCanExecuteChanged();
}
}
答案 0 :(得分:0)
除非您明确创建存储应用程序的文件“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);
}
}
}
}
编辑:除非您创建存储应用程序的文件“collections.xml”,否则您的read方法将永远不会工作,请使用以下内容:
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++;
}
}
}
答案 1 :(得分:-1)
您不需要使用文件流。
只做一个collection.Save(filename);
编辑: 您是否打算尝试将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