I've been working on an application that loads and writes configuration to XML files. I know there are some opinions about doing this, but I've been having trouble with this code.
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();
}
}
This entire function is called in a foreach loop in another function, and all the function should to is check to see if there is a configuration file in the folder, check for html files, ask the user for information about the files, and then save out to the XML file.
I'm thinking that I need to move the Filestream manipulation, and possibly the XDocument to the calling function, and pass them into this one.
The big issue is it only saves the last set of nodes.