0

I am trying to create an XML file with the following structure

enter image description here

However, this is what I get (one line):

<?xml version="1.0" encoding="utf-8"?><!--This file is generated by the program.--><root><OFBM time="9:15" date="22.06.2016"><folder>file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client</folder><folder>file:///C:/Arduino223</folder></OFBM></root>

My code:

public void CreateXML()
        {
            XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");

            writer.WriteStartDocument();

            writer.WriteComment("This file is generated by the program.");

            writer.WriteStartElement("root");
            writer.WriteStartElement("OFBM");
            writer.WriteAttributeString("time", "9:15");
            writer.WriteAttributeString("date", "22.06.2016");
            writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
            writer.WriteElementString("folder", @"file:///C:/Arduino223");
            writer.WriteEndElement();
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Flush();
            writer.Close();
        }

How do I also append new data? Instead of creating a new XML each time.

Alex
  • 1,210
  • 3
  • 21
  • 34
  • 1
    Look this article - http://stackoverflow.com/questions/4094180/indentation-and-new-line-command-for-xmlwriter-in-c-sharp. There is a solution for adding indentation. Second issue: http://stackoverflow.com/questions/20922835/appending-an-existing-xml-file-with-xmlwriter - how to add xml to existing file. – VitaliyK Jan 14 '17 at 22:47

1 Answers1

0

Add some settings to achieve the indent you are trying to accomplish in your XML file:

 //I added this part (XmlWriterSettings):
 XmlWriterSettings mySettings = new XmlWriterSettings();
 mySettings.Indent = true;
 mySettings.IndentChars = ("    ");
 mySettings.CloseOutput = true;
 mySettings.OmitXmlDeclaration = true;

//Added the settings you intialize and set on your XmlWriter:
 XmlWriter writer = XmlWriter.Create(@"C:\Users\Alek\Dropbox\D\Debug\product.xml", mySettings);

 writer.WriteStartDocument();

 writer.WriteComment("This file is generated by the program.");

 writer.WriteStartElement("root");
 writer.WriteStartElement("OFBM");
 writer.WriteAttributeString("time", "9:15");
 writer.WriteAttributeString("date", "22.06.2016");
 writer.WriteElementString("folder", @"file:///C:/Program Files (x86)/Cisco/Cisco AnyConnect Secure Mobility Client");
 writer.WriteElementString("folder", @"file:///C:/Arduino223");
 writer.WriteEndElement();
 //writer.WriteEndElement();
 //writer.WriteEndDocument();
  writer.Flush();
  writer.Close();

To append new data without recreating the file, you can do this:

 XDocument doc = XDocument.Load(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
 XElement school = doc.Element("root");
 school.Add(new XElement("OFBM",
                   new XElement("folder", "file:///C:/Arduino223")));
 doc.Save(@"C:\Users\Alek\Dropbox\D\Debug\product.xml");
Willy David Jr
  • 8,604
  • 6
  • 46
  • 57