Hey guys,
Im looking for a nicer way to replace this code:
XmlDocument doc = new XmlDocument();
doc.Load(System.Windows.Forms.Application.StartupPath + @"\groups.xml");
XmlNode newNode = doc.CreateNode(XmlNodeType.Element, "GROUP", "");
string tmp = @"
for (int i = 0; i < arr.Count; i++)
tmp += @"
newNode.InnerXml = tmp;
doc.DocumentElement.AppendChild(newNode);
doc.Save(System.Windows.Forms.Application.StartupPath + @"\groups.xml");
this should edit an xml document with root named
Scott LyslePosted Dec 8, 2007, 1:26 PM
You could use the XmlTextWriter (System.Xml); this will produce a similar example and shows a little bit of what you can do with it:
private void button1_Click(object sender, EventArgs e) { // create the xml text writer instance XmlTextWriter writer = new XmlTextWriter("c:\\temp\\junk.xml", null); // Write out the content of the report into the xml document try { //Setup writer.Formatting = Formatting.Indented; writer.Indentation = 5; writer.Namespaces = true; //Report Header writer.WriteStartDocument(); writer.WriteComment("Groups Example"); writer.WriteComment("Generation Date: " + DateTime.Now.ToShortDateString()); writer.WriteComment("Generation Time: " + DateTime.Now.ToShortTimeString()); writer.WriteStartElement("GROUPS"); writer.WriteStartElement("GROUP"); for (int i = 0; i < 3; i++) { writer.WriteStartElement("Name"); writer.WriteString(i.ToString()); for (int j = 0; j < 10; j++) { writer.WriteStartElement("Value"); writer.WriteString(j.ToString()); writer.WriteEndElement(); } writer.WriteEndElement(); } writer.WriteEndElement(); writer.WriteEndElement(); writer.WriteEndDocument(); writer.Flush(); writer.Close(); } catch (Exception ex) { MessageBox.Show(ex.ToString(), "Error"); writer.Close(); } }