title: There is an error in XML document (2, 2): how to deserialize this xml?
body: <p>I have a xml-document that needs to be deserialized into an object with a <code>XmlSerializer</code>:</p>
  
  <p>The method that does this work takes a default namespace and a xml string:</p>
  
  <pre><code>public static T Deserialize(string xml, string defaultNamespace)
  {
      var serializer = new XmlSerializer(typeof(T), defaultNamespace);
      object obj;
      using (var stringReader = new StringReader(xml))
      {
          obj = serializer.Deserialize(stringReader);
          stringReader.Close();
      }
      return (T)obj;
  }
  </code></pre>
  
  <p>When the default namespace "<a href="http://www.defaultnamespace.com/xsd/MyObject12" rel="nofollow">http://www.defaultnamespace.com/xsd/MyObject12</a>" is passed with the following xml string: (where new lines are "\n\r")</p>
  
  <pre><code>&lt;xml version="1.0" encoding="utf-8"?&gt;
  &lt;ns:MyObject12 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
               xmlns:ns1="http://example.com/xsd/example11" 
               xmlns:xs="http://www.w3.org/2001/XMLSchema" 
               xmlns:ns="http://www.example.com/xsd/MyObject12"&gt;
    &lt;Status xmlns="some:stuff"&gt;
      &lt;StatusCode&gt;0&lt;/StatusCode&gt;
    &lt;/Status&gt;
  &lt;/ns:MyObject12&gt;
  </code></pre>
  
  <p>the following exception is thrown: </p>
  
  <blockquote>
    <p>An exception of type 'System.InvalidOperationException' occurred in System.Xml.dll but was not handled in user code<br><br>Additional information: There is an error in XML document (2, 2).<br><br>Inner exception: {&lt; MyObject12 xmlns='<a href="http://www.example.com/xsd/MyObject12" rel="nofollow">http://www.example.com/xsd/MyObject12</a>'> was not expected."}</p>
  </blockquote>
  
  <p>I tried to fix it by prepending  <code>ns:</code> to all xml elements, but the same error occured.</p>
  
  <p>Does anyone know how this can be fixed?</p>