title: XmlDocument Save keeps file open
body: <p>I have a simple c# function that creates a basic XML file and saves:</p>
  
  <pre><code>private void CreateXMlFile(string Filename, string Name, string Company)
          {
              XmlDocument doc = new XmlDocument();
              XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
              doc.AppendChild(docNode);
  
              XmlNode licenseNode = doc.CreateElement("license");
              doc.AppendChild(licenseNode);
  
              XmlNode node = doc.CreateElement("Name");
              node.AppendChild(doc.CreateTextNode(Name));
              licenseNode.AppendChild(node);
  
              node = doc.CreateElement("Company");
              node.AppendChild(doc.CreateTextNode(Company));
              licenseNode.AppendChild(node);
  
  
              doc.Save(Filename);
          }
  </code></pre>
  
  <p>When I try to edit or delete the file I always get following error:</p>
  
  <blockquote>
    <p>The process cannot access the file because it is being used by another
    process.</p>
  </blockquote>
  
  <p>XmlDocument doesnt have any inbuilt dispose or close routines and wondered how I can force the file to close before later editing or deleting it.</p>
  
  <p>I have tried to save the file using StreamWriter:</p>
  
  <pre><code>StreamWriter outStream = System.IO.File.CreateText(outfile);
              outStream.Write(data);
              outStream.Close();
  </code></pre>
  
  <p>But this didnt make a difference with the same error.</p>
  
  <p>Your advice is greatly accepted.</p>
  
  <p>Thank you</p>