title: Add new lines to XML file
body: <p>I have a program that creates an XML log. First I check if the XML exists, and if not I use the following code to create it:</p>
  
  <pre><code>XmlWriterSettings settings = new XmlWriterSettings();
  settings.Indent = true;
  
  using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
                      {
                      writer.WriteStartDocument();
                      writer.WriteComment("Comment here.");
                      writer.WriteStartElement("Batch");
                      writer.WriteAttributeString("ID", BatchID);
                      writer.WriteAttributeString("Date", now);
                      writer.WriteStartElement("Step");
                      writer.WriteElementString("Name", step);
                      writer.WriteElementString("Success", success);
                      writer.WriteElementString("Message", message);
                      writer.WriteStartElement("Transaction");
                      writer.WriteElementString("ID", transID);
                      writer.WriteElementString("Details", details);
  
                      writer.WriteEndElement();
                      writer.WriteEndDocument();
                  }
  </code></pre>
  
  <p>This works as expected and creates the file:</p>
  
    
  
  <pre><code>&lt;!--This log was created by the Application.--&gt;
  &lt;Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93"&gt;
  &lt;Step&gt;
     &lt;Name&gt;Start Batch&lt;/Name&gt;
     &lt;Success&gt;1&lt;/Success&gt;
     &lt;Message&gt;Processing Batch&lt;/Message&gt;
  &lt;Transaction&gt;
     &lt;ID&gt;N/A&lt;/ID&gt;
     &lt;Details&gt;N/A&lt;/Details&gt;
  &lt;/Transaction&gt;
  &lt;/Step&gt;
  </code></pre>
  
  <p>  </p>
  
  <p>So the second time around, it checks that the XML exists, it does, so instead of creating it  should just add to it. I'm using the following code for this:</p>
  
  <pre><code>                XDocument xmlDoc = XDocument.Load(myXMLLog);
                  XElement root = new XElement("Batch");
  
                  root.Add(new XAttribute("ID", BatchID));
                  root.Add(new XAttribute("Date", now));
                  root.Add(new XElement("Step"));
                  root.Add(new XAttribute("Name", step));
                  root.Add(new XAttribute("Success", success));
                  root.Add(new XAttribute("Message", message));
                  root.Add(new XElement("Transaction"));
                  root.Add(new XAttribute("ID", transID));
                  root.Add(new XAttribute("Details", details));
  
                  xmlDoc.Element("Batch").Add(root);
                  xmlDoc.Save(myXMLLog);
  </code></pre>
  
  <p>So now I'm expecting the XML file to look something like this:</p>
  
    
  
  <pre><code>&lt;!--This log was created by the Application.--&gt;
  &lt;Batch Date="1/22/2014 10:01:11 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d59a93"&gt;
  &lt;Step&gt;
     &lt;Name&gt;Start Batch&lt;/Name&gt;
     &lt;Success&gt;1&lt;/Success&gt;
     &lt;Message&gt;Processing Batch&lt;/Message&gt;
  &lt;Transaction&gt;
     &lt;ID&gt;N/A&lt;/ID&gt;
     &lt;Details&gt;N/A&lt;/Details&gt;
  &lt;/Transaction&gt;
  &lt;/Step&gt;
  &lt;/Batch&gt;
  &lt;Batch Date="1/22/2014 10:20:00 PM" ID="166bf0d2-3bd4-4353-b309-20b6d0d5aaa"&gt;
  &lt;Step&gt;
     &lt;Name&gt;Start Batch&lt;/Name&gt;
     &lt;Success&gt;1&lt;/Success&gt;
     &lt;Message&gt;Processing Batch&lt;/Message&gt;
  &lt;Transaction&gt;
     &lt;ID&gt;N/A&lt;/ID&gt;
     &lt;Details&gt;N/A&lt;/Details&gt;
  &lt;/Transaction&gt;
  &lt;/Step&gt;
  &lt;/Batch&gt;
  </code></pre>
  
  <p>But instead I get an exception "Duplicate Attribute".</p>
  
  <p>What am I doing wrong?</p>
  
  <p>Thanks!</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The XML Document does has  at the top of it. Valid XML would be very nice, but not a most.</p>
  
  <p><strong>EDIT EDIT:</strong></p>
  
  <p>This is how my code looks now:</p>
  
  <pre><code>            if (File.Exists(myXMLLog))
              {              
                  XDocument xmlDoc = XDocument.Load(myXMLLog);
                  XElement root = new XElement("Batch",
                      new XAttribute("ID", BatchID),
                      new XAttribute("Date", now),
                      new XElement("Step",
                          new XElement("Name", step),
                          new XElement("Success", success),
                          new XElement("Message", message),
                          new XElement("Transaction",
                              new XAttribute("IDTrans", transID),
                              new XAttribute("Details", details))));
                  xmlDoc.Root.Add(root);
                  xmlDoc.Save(myXMLLog);
              }
              else
              {
                  using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
                  {
                      writer.WriteStartDocument();
  
                      writer.WriteComment("This log was created by the Application.");
                      writer.WriteStartElement("Batch");
                      writer.WriteAttributeString("ID", BatchID);
                      writer.WriteAttributeString("Date", now);
                      writer.WriteStartElement("Step");
                      writer.WriteElementString("Name", step);
                      writer.WriteElementString("Success", success);
                      writer.WriteElementString("Message", message);
                      writer.WriteStartElement("Transaction");
                      writer.WriteElementString("IDTrans", transID);
                      writer.WriteElementString("Details", details);
  
                      writer.WriteEndElement();
                      writer.WriteEndDocument();
                  }
              }
  </code></pre>
  
  <p>I'm ok with the format of the XML, my problem now is that is doing the BATCH inside the first BATCH</p>
  
  <pre><code>&lt;Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42"&gt;
  &lt;Step&gt;&lt;Name&gt;Create DataTable&lt;/Name&gt;
  &lt;Success&gt;1&lt;/Success&gt;
  &lt;Message&gt;DataTable was Created and Populated&lt;/Message&gt;
  &lt;Transaction&gt;
  &lt;IDTrans&gt;N/A&lt;/IDTrans&gt;
  &lt;Details&gt;0&lt;/Details&gt;
  &lt;/Transaction&gt;
  &lt;/Step&gt;
  &lt;Batch Date="1/22/2014 11:49:17 PM" ID="6966578b-b326-4f16-a315-1b4228a9fa42"&gt;
  &lt;Step&gt;
  &lt;Name&gt;Send Email&lt;/Name&gt;
  &lt;Success&gt;1&lt;/Success&gt;
  &lt;Message&gt;Template Seleted and Filled&lt;/Message&gt;
  &lt;Transaction Details="Email To: email@email.com Link:  " IDTrans="243b0a3c-d8b7-49c3-b1d0-asdfsdsdfsdf"/&gt;
  &lt;/Step&gt;
  &lt;/Batch&gt;.....
  </code></pre>
  
  <p><strong><em>EDIT FINAL</em></strong></p>
  
  <p>Working Code, combination of all answers:</p>
  
  <pre><code>            if (File.Exists(myXMLLog))
              {              
                  XDocument xmlDoc = XDocument.Load(myXMLLog);
                  XElement root = new XElement("Batch",
                      new XAttribute("ID", BatchID),
                      new XAttribute("Date", now),
                      new XElement("Step",
                          new XElement("Name", step),
                          new XElement("Success", success),
                          new XElement("Message", message),
                          new XElement("Transaction",
                              new XAttribute("IDTrans", transID),
                              new XAttribute("Details", details))));
                  xmlDoc.Root.Add(root);
                  xmlDoc.Save(myXMLLog);
              }
              else
              {
                  using (XmlWriter writer = XmlWriter.Create(myXMLLog, settings))
                  {
                      writer.WriteStartDocument();
                      writer.WriteComment("This log was created by the Application.");
                      writer.WriteStartElement("Root");
                      writer.WriteStartElement("Batch");
                      writer.WriteAttributeString("ID", BatchID);
                      writer.WriteAttributeString("Date", now);
                      writer.WriteStartElement("Step");
                      writer.WriteElementString("Name", step);
                      writer.WriteElementString("Success", success);
                      writer.WriteElementString("Message", message);
                      writer.WriteStartElement("Transaction");
                      writer.WriteElementString("IDTrans", transID);
                      writer.WriteElementString("Details", details);
                      writer.WriteEndElement();
                      writer.Flush();
                      writer.WriteEndDocument();
                  }
              }
  </code></pre>
  
  <p>I'm happy with the results. If anyone has suggestions on how to better format the XML, please do so. Thanks everyone.</p>