title: C# write to XML error
body: <p>I am having trouble writing to my XML file</p>
  
  <p>Here is my code:</p>
  
  <pre><code>path = test.xml
  
  
  FileStream READER = new FileStream(path, FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
  System.Xml.XmlDocument Template = new System.Xml.XmlDocument();
  Template.Load(READER);
  
  //WRITE TO XML
  
  FileStream WRITER = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite); //Set up the filestream (READER) //
  Template.Save(WRITER);
  </code></pre>
  
  <p>It works the first time i click the button but then if i click it again i get the error </p>
  
  <p>xmlexception handle Data at the root level is invalid. Line 87, position 10.</p>
  
  <p>is this because the xml document is not closed? if so how do i go about doing this</p>
  
  <p>Please can someone help me</p>
  
  <p><em><strong></em>**<em>*</em>**</strong> UPDATE <strong><em>*</em>**<em>*</em>*</strong></p>
  
  <p>I've now gotten it work.
  Just for those who may also be struggling with this here is my new code:</p>
  
  <p>path = test.xml</p>
  
  <pre><code>using(FileStream READER = new FileStream(path, FileMode.Open,FileAccess.Read,FileShare.ReadWrite))
  {
      System.Xml.XmlDocument Template = new System.Xml.XmlDocument();
      Template.Load(READER);
  
      //WRITE TO XML
  
      using(FileStream WRITER = new FileStream(path, FileMode.Open, FileAccess.Write, FileShare.ReadWrite))
      {
      Template.Save(WRITER);
      }
  }
  </code></pre>