title: How to remove declaration and add root element in existing XML file (C#)
body: <p>I'm fairly new to XML and C#, so please understand if this question is too sily to ask.</p>
  
  <p>I'm converting XML format using C# win-form application. The app opens a xml file using <code>OpenFileDialog</code>, then conversion will be excuted(this is already done, but I still need to add or remove some more like below) . After conversion, the app will save the modified xml file using <code>SaveFileDialog</code>.</p>
  
  <p>Original XML Format </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; 
     &lt;DataList&gt;
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt; 
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt;
       ..&lt;Data&gt;&lt;/Data&gt; continued...
     &lt;/DataList&gt;
  </code></pre>
  
  <p>I want to edit the XML file as below </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8" ?&gt; **→ Remove this delaration!**
   &lt;MainInterface&gt; **→ Add 'root element' before existing nodes**
     &lt;DataList&gt;
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt; 
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt;
      &lt;Data&gt;
       &lt;ID&gt;1&lt;/ID&gt;
       &lt;Name&gt;Mike&lt;/Name&gt;
       &lt;Age&gt;23&lt;/Age&gt;
      &lt;/Data&gt;
       ..&lt;Data&gt;&lt;/Data&gt; continued...
     &lt;/DataList&gt;
   &lt;/MainInterface&gt; **→ close newly added root element**
  </code></pre>
  
  <p>I've tried below code but seem like it doesn't work</p>
  
  <pre><code>OpenFileDialog openFileDialogue = new OpenFileDialog();           
  openFileDialog1.DefaultExt = "xml";
  openFileDialog1.Filter = "xml files (*.xml)|*.xml";
  openFileDialog1.Title = "Select a xml File";
  openFileDialog1.ShowDialog();
  
  XDocument xmlFile = XDocument.Load(openFileDialog1.FileName);
  **// Remove Declaration**
  XDocument doc = new XDocument(new XDeclaration(null, null, null));
  
  **// Add Root Element**
  XElement doc1 = XElement.Parse(openFileDialog1.FileName);
  XElement root = new XElement("MainInterface", doc1);
  //doc.Save(_data)
  openFileDialog1.FileName = root.ToString();
  
  -----------------------------------------------------------------------------------
  Do something for conversion ~~~
  -----------------------------------------------------------------------------------
  SaveFileDialog saveFileDialogue1 = new SaveFileDialog();
  saveFileDialog1.Filter = "xml File |*.xml";
  saveFileDialog1.Title = "Conversion Completed! Save a XML file";
  saveFileDialog1.FileName = "XML Converted.xml";            
  saveFileDialog1.ShowDialog();
  xmlFile.Save(saveFileDialog1.FileName);
  </code></pre>
  
  <p>The point is I'm not creating new XML file, but modifying existing xml file to get rid of declaration and add a root element. Should I use XML Writer? Is there simpler way of doing these? Thank you in advance.</p>
  
  <p><em><strong>Thank you for your answers. I found out this works for me!</em></strong></p>
  
  <pre><code>SaveFileDialog saveFileDialogue1 = new SaveFileDialog();
  saveFileDialog1.Filter = "xml File |*.xml";
  saveFileDialog1.Title = "Conversion Completed! Save a XML file";
  saveFileDialog1.FileName = "XML Converted.xml";            
  saveFileDialog1.ShowDialog();
  
  XmlWriterSettings settings = new XmlWriterSettings();
  settings.OmitXmlDeclaration = true;
  StringWriter sw = new StringWriter();
  using (XmlWriter xw = XmlWriter.Create(saveFileDialog1.FileName, settings))
     {
        xmlFile.Save(xw);
     }
  string s = sw.ToString();
  </code></pre>