保存XML文件似乎只能保存一个条目
我一直在处理将配置加载和写入XML文件的应用程序。我知道这样做有一些意见,但我一直在这个代码有问题。保存XML文件似乎只能保存一个条目
private static void AddToXmlTemplate(Template tmp, string _config)
{
string configFile = _config + "configuredTemplate.xml";
FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate);
if (File.Exists(configFile)) {
XDocument xD = new XDocument();
xD.Add(new XElement("Store",
new XElement("template",
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc))));
xD.Save(fs);
fs.Flush();
fs.Dispose();
//commenting for change to allow sync.
}
else
{
/********------ appends the template to the config file.------*************/
XDocument xD = XDocument.Load(fs);
XElement root = xD.Element("Store");
IEnumerable<XElement> rows = root.Descendants("template");
XElement last = rows.Last();
last.AddAfterSelf(
new XElement("template"),
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc));
xD.Save(fs);
fs.Flush();
fs.Dispose();
}
}
这整个函数被调用另一个函数foreach循环,并且所有的功能应是检查,看看是否有文件夹中的配置文件,检查HTML文件,要求用户提供信息关于这些文件,然后保存到XML文件中。
我在想,我需要将文件流操作,也可能是XDocument移动到调用函数,并将它们传递给这个。
最大的问题是它只保存最后一组节点。
我认为问题出在if (!File.Exists(configFile))
。你可以试试这个:
private static void AddToXmlTemplate(Template tmp, string _config)
{
string configFile = Path.Combine(_config, "configuredTemplate.xml");
using (FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate))
{
if (!File.Exists(configFile))
{
XElement xD = new XElement("Store",
new XElement("template"),
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc));
xD.Save(fs);
fs.Flush();
}
else
{
XDocument xD = XDocument.Load(fs);
XElement root = xD.Element("Store");
IEnumerable<XElement> rows = root.Descendants("template");
XElement last = rows.Last();
last.AddAfterSelf(
new XElement("template"),
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc));
xD.Save(fs);
fs.Flush();
}
}
}
你的if语句的逻辑肯定是错误的?
当前,如果文件存在,那么你正在创建一个新的XML文件,如果没有,你需要添加它,它需要是另一种方式。
如果改成这样它应该工作
if (!File.Exists(configFile)) {
必须存在一些其他奇怪的逻辑问题,因为我原来是这样的,但它只运行第二个块。 –
啊我认为它是因为你正在创建文件流,然后检查它是否存在。请看我的其他答案。 – toby
啊我想它的,因为你是第一个创建的文件流,然后检查是否存在这样它就会一直存在。如果您将其更改为
private static void AddToXmlTemplate(Template tmp, string _config)
{
string configFile = _config + "configuredTemplate.xml";
if (!File.Exists(configFile)) {
FileStream fs = new FileStream(configFile, FileMode.OpenOrCreate)
XDocument xD = new XDocument();
xD.Add(new XElement("Store",
new XElement("template",
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc))));
xD.Save(fs);
fs.Flush();
fs.Dispose();
//commenting for change to allow sync.
}
else
{
FileStream fs = new FileStream(configFile, FileMode.Open);
/********------ appends the template to the config file.------*************/
XDocument xD = XDocument.Load(fs);
XElement root = xD.Element("Store");
IEnumerable<XElement> rows = root.Descendants("template");
XElement last = rows.Last();
last.AddAfterSelf(
new XElement("template"),
new XElement("filePath", tmp.TempPath),
new XElement("Name", tmp.TempName),
new XElement("description", tmp.TempDesc));
xD.Save(fs);
fs.Flush();
fs.Dispose();
}
}
这样做更有意义,但是您能否确定fs和xD变量是否已妥善保存/处理以便下次运行? –
您正在处理正确的FileStream。另一种方法是使用“using”(http://stackoverflow.com/questions/212198/what-is-the-c-sharp-using-block-and-why-should-i-use-it) – toby
谢谢!现在我只需要解决更新块添加新的XML声明项的问题。 –
我可以看到这应该如何工作,但是如果我们进行这些更改,它只会运行else块。 –
@ChrisRutherford可能与文件路径有关。尝试使用字符串configFile = Path.Combine(_config,“configuredTemplate.xml”) – daniell89