Skip to content

Instantly share code, notes, and snippets.

@mbrownnycnyc
Last active December 15, 2015 13:09
Show Gist options
  • Save mbrownnycnyc/5264826 to your computer and use it in GitHub Desktop.
Save mbrownnycnyc/5264826 to your computer and use it in GitHub Desktop.
a c# function to write an XML; was written for generation of a config file for Microsoft Research's Management of Access Control in the Enterprise (MACE). I will be publishing the repo (a wrapper for the datacollector executable in the MACE suite) when it is sufficiently functional.
public static void WriteXML(string filename, Dictionary<string, string> commandoptions)
{
//if the file exists, then move it to DataCollector.exe.config.bak
if (File.Exists(filename))
{
File.Move(filename, filename + ".bak");
}
//write the XML document//
// http://csharp.net-informations.com/xml/how-to-create-xml.htm
// http://www.dotnetperls.com/xmltextwriter
// using XMLWriter over XMLTextWriter: http://blogs.msdn.com/b/xmlteam/archive/2011/10/08/the-world-has-moved-on-have-you-xml-apis-you-should-avoid-using.aspx
// http://msdn.microsoft.com/en-us/library/kkz7cs0d.aspx
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = (" ");
using (XmlWriter writer = XmlWriter.Create(filename, settings))
{
// Write XML data.
writer.WriteStartElement("configuration");
writer.WriteStartElement("appSettings");
foreach (KeyValuePair<string, string> option in commandoptions)
{
writer.WriteStartElement("add");
writer.WriteAttributeString("key", option.Key);
writer.WriteAttributeString("value", option.Value);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndElement();
writer.Flush();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment