title: IOException The process cannot access the file because it is being used by another process when using XDocument
body: <p>I keep getting an IOException that it cannot access the file because it's being used by another process.  What i'm trying to do is that everytime the file i'm looking at is changed.. it's sending it as an array through TCP/IP.  i couldn't find any way of closing the XDocument and just don't know how to fix this error... i google'd and still couldn't find anything.  any help would be grateful</p>
  
  <p>edit: i found other solutions with filereader and other things.. but it seems different when using xdocument</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  
  //filesystemwatcher
  using System.IO;
  
  //tcpip server
  using System.Net;
  using System.Net.Sockets;
  
  //XML
  using System.Xml;
  using System.Xml.Linq;
  
  namespace ChampSelect_FileWatcher
  {
      class Program
      {
          //TCP IP variables
          public static Int32 port;
          public static IPAddress localAddr;
          public static TcpListener server;
          public static TcpClient client;
          public static NetworkStream stream;
          public static XDocument doc;
  
  
          public static void Main(string[] args)
          {
              //LOAD XML FILE
              doc = XDocument.Load("C:/Trio Scripts/example.xml");
  
              //OBSERVE FILE
              FileSystemWatcher watcher = new FileSystemWatcher();
              watcher.Path = @"C:\Trio Scripts";
              watcher.Filter = "example.xml";
  
              //watch for changes in LastWrite
              watcher.NotifyFilter = NotifyFilters.LastWrite;
  
              //event handler
              watcher.Changed += new FileSystemEventHandler(OnChanged);
  
              //Begin watching
              watcher.EnableRaisingEvents = true;
  
  
              //TCP IP SERVER
              try
              {
                  Console.WriteLine("Waiting for 99150 to run...\n");
                  //config TCP stuff
                  port = 9905;
                  localAddr = IPAddress.Parse("10.0.0.66");
                  server = new TcpListener(localAddr, port);
                  server.Start();
                  client = server.AcceptTcpClient();
                  stream = client.GetStream();
                  Console.WriteLine("Connection to Viz successful!");
                  Console.WriteLine("***LISTENING FOR CHANGES TO: " + watcher.Filter + "***\n");
  
  
              }
              catch (SocketException z)
              {
                  Console.WriteLine("SocketException: {0}", z);
              }
  
              //prevent console from closing
              Console.ReadKey();
              Console.ReadKey();
              Console.ReadKey();
          }
  
          // Define the event handlers.
          private static void OnChanged(object source, FileSystemEventArgs e)
          {
              //reload xml file
              //doc = XDocument.Load("C:/Trio Scripts/example.xml");
              //doc.Root.ReplaceWith(XElement.Load("C:/Trio Scripts/example.xml"));
  
              XDocument doc;
              using (var reader = XmlReader.Create("C:/Trio Scripts/example.xml"))
              {
                  doc = XDocument.Load(reader);
              }
  
              // Nodes in XML
              string[] bans = doc.Descendants("ban").OrderBy(element =&gt; Int32.Parse(element.Attribute("order").Value)).Select(element =&gt; element.Value).ToArray();
  
              // String to send the message on
              String sendMsg = "";
  
              // Proceed with reading XML
              for (int i = 0; i &lt; bans.Length; i++)
                  sendMsg += bans[i] + " ";
  
              byte[] msg = System.Text.Encoding.ASCII.GetBytes(sendMsg);
              stream.Write(msg, 0, msg.Length);
              Console.WriteLine("Change detected, sending changes to Viz");
              sendMsg = "";
          }
      }//end class
  }//end namespace
  </code></pre>