title: C# file still in use after a using statement
body: <p>The file in the following function is still in use after the using statement. How can I fix this to release the file....</p>
  
  <pre><code>/// &lt;summary&gt;
      /// Serializes an object to an xml file.
      /// &lt;/summary&gt;
      /// &lt;param name="obj"&gt;
      /// The object to serialize.
      /// &lt;/param&gt;
      /// &lt;param name="type"&gt;
      /// The class type of the object being passed.
      /// &lt;/param&gt;
      /// &lt;param name="fileName"&gt;
      /// The filename where the object should be saved to.
      /// &lt;/param&gt;
      /// &lt;param name="xsltPath"&gt;
      /// Pass a null if not required.
      /// &lt;/param&gt;
      public static void SerializeToXmlFile(object obj, Type type, string fileName, string xsltPath )
      {
          var ns = new XmlSerializerNamespaces();
          ns.Add(String.Empty, String.Empty);
          var serializer = new XmlSerializer(type);
  
          var settings = new XmlWriterSettings {Indent = true, IndentChars = "\t"};
  
  
          using (var w = XmlWriter.Create(File.Create(fileName), settings))
          {
  
              if (!String.IsNullOrEmpty(xsltPath))
              {
                  w.WriteProcessingInstruction("xml-stylesheet", "type=\"text/xsl\" href=\"" + xsltPath + "\"");
  
              }
              serializer.Serialize(w, obj, ns);
          }
      }
  </code></pre>