Im making a config file to a C# program in XML. I have save and load
functions, but these require a base XML file with its structure to
exsist (Im new to XML, so this was the way I it to work).
Now I want to extend that, so if the base XML file doesnt exsist a
function will make the structure. However this way seem somewhat
troublesome to do it, but it works.
Is there an easier way?
Also is it allowed to use space in the element name ?
C# code:
System.Xml.XmlDocument xDoc = new System.Xml.XmlDocument();
System.Xml.XmlNode root;
System.Xml.XmlNode node;
System.Xml.XmlNode child, child2, child3;
root = xDoc.CreateElement("Configuration");
xDoc.AppendChild(root);
node = xDoc.CreateElement("Application");
child = xDoc.CreateElement("Name");
child.InnerText = "some value";
node.AppendChild(child);
child = xDoc.CreateElement("Version");
child.InnerText = "some value";
node.AppendChild(child);
child = xDoc.CreateElement("Author");
child.InnerText = "some value";
node.AppendChild(child);
root.AppendChild(node);
node = xDoc.CreateElement("Options");
child = xDoc.CreateElement("Main");
child2 = xDoc.CreateElement("BackupDir");
child.AppendChild(child2);
node.AppendChild(child);
child = xDoc.CreateElement("Settings");
child2 = xDoc.CreateElement("SourceFolders");
child3 = xDoc.CreateElement("Folder");
child3.InnerText = string.Empty;
child2.AppendChild(child3);
child.AppendChild(child2);
child2 = xDoc.CreateElement("Filters");
child3 = xDoc.CreateElement("Filter");
child3.InnerText = string.Empty;
child2.AppendChild(child3);
child.AppendChild(child2);
child2 = xDoc.CreateElement("Recursive");
child.AppendChild(child2);
child2 = xDoc.CreateElement("Overwrite");
child.AppendChild(child2);
child2 = xDoc.CreateElement("CheckForFreeSpace");
child.AppendChild(child2);
node.AppendChild(child);
root.AppendChild(node);
xDoc.Save(filename); //filename is a string passed to this
function with a filename and its extension
This gives an XML as listed below:
<Configuration>
<Application>
<Name>some value</Name>
<Version>some value</Version>
<Author>some value</Author>
</Application>
<Options>
<Main>
<BackupDir />
</Main>
<Settings>
<SourceFolders>
<Folder // This should be on 1 line as <Folder></Folder>
</Folder //
</SourceFolders>
<Filters>
<Filter // This should be on 1 line as <Filter></Filter>
</Filter //
</Filters>
<Recursive / // This should be as <Recursive></Recursive>
<Overwrite / // This should be as <Overwrite></Overwrite>
<CheckForFreeSpace / // This should be as
<CheckForFreeSpace></CheckForFreeSpace>
</Settings>
</Options>
</Configuration>
I need to add at least 1 child on both <SourceFoldersand <Filters>,
or the save/load functions dont work.