title: Error Deserializing XML (C#): There is an error in XML document (2, 2) (but XmlSerializer created it)
body: <p>I know, I know questions on failure to deserialize XML with error "There is an error in XML document (2, 2)." are littered across the internet but I'm still stumped.</p>
  
  <p>I have an XML document created using XmlSerializer from a c# class. This document is failing to deserialize with Exception:</p>
  
  <pre><code>There is an error in XML document (2, 2).
  Inner Exception: The server could not be contacted.
  Root inner exception: The LDAP server is unavailable.
  </code></pre>
  
  <p>I am using XmlSerializer to both serialise and deserialize and strangely, the XML document in question can be deserialized using the same code on another computer. </p>
  
  <p>I use the following code to serialise and deserialize classes I have generated in C#:</p>
  
  <pre><code>    public static T DeserializeObject&lt;T&gt;(string filename)
      {
          Console.WriteLine("Reading with XmlReader");
  
          // Create an instance of the XmlSerializer specifying type and namespace.
          XmlSerializer serializer = new
          XmlSerializer(typeof(T));
  
          // A FileStream is needed to read the XML document.
          FileStream fs = new FileStream(filename, FileMode.Open);
          xml.XmlReader reader = xml.XmlReader.Create(fs);
  
          // Declare an object variable of the type to be deserialized.
          T i;
  
          // Use the Deserialize method to restore the object's state.
          i = (T)serializer.Deserialize(reader);
          fs.Close();
          return i;
      }
  
      public static void WriteObject&lt;T&gt;(T value, string filename)
      {
          XmlSerializer serializer = new XmlSerializer(typeof(T));
          TextWriter tw = new StreamWriter(filename);
          serializer.Serialize(tw, value);
          tw.Close();
      }
  </code></pre>
  
  <p>The XML document is stored locally at:
  C:\Users\username\AppData\Local\myapp\myxmldoc.xml</p>
  
  <p>and follows this format:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;MyClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
    &lt;Id xsi:nil="true" /&gt;
    &lt;Property1&gt;text&lt;/Property1&gt;
    &lt;Property2&gt;0000000&lt;/Property2&gt;
    &lt;Property3&gt;
      &lt;Property31&gt;
        &lt;Property311&gt;text&lt;/Property311&gt;
        &lt;Property312&gt;text&lt;/Property312&gt;
      &lt;/Property31&gt;    
    &lt;/Property3&gt;  
    &lt;Property4 /&gt;
    &lt;Property5&gt;false&lt;/Property5&gt;
  &lt;/MyClass&gt;
  </code></pre>
  
  <p>I use the following code to deserialize:</p>
  
  <pre><code>myclassvar = SerialiseXML.DeserializeObject&lt;MyClass&gt;(xmldocpath);
  </code></pre>
  
  <p>Other classes are serialising to, and deserializing from, XML just fine on the offending computer.</p>
  
  <p>Oh and for kicks I ran the code through a validator which found no faults.</p>
  
  <p>Any ideas?</p>