title: Why must I remove the xmlns attribute to deserialize my XML file?
body: <p><strong>Problem</strong></p>
  
  <p>I'm using <a href="http://www.codeplex.com/Xsd2Code" rel="nofollow noreferrer">Xsd2Code</a> (a .NET class generator for XSD schema) on a simple settings file schema. For some reason, when I attempt to use the built-in <code>LoadFromFile()</code> or <code>Deserialize()</code> methods, I get an exception that seems to be related to the <code>xmlns</code> attributes in my XSD and XML files. If I remove these attributes, the exception goes away. (Please see my "<strong>Update</strong>" below for details on the code that is actually causing the exception.)</p>
  
  <hr>
  
  <p><strong>XSD File</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xs:schema
    xmlns="Myco.CLDatabaseBuilder.Models"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    targetNamespace="Myco.CLDatabaseBuilder.Models"
    elementFormDefault="qualified"&gt;
    &lt;xs:element name="Settings" type="Settings" /&gt;
    &lt;xs:complexType name="Settings"&gt;
      &lt;xs:sequence&gt;
        &lt;xs:element name="SqlServerInstanceName" type="xs:string" /&gt;
        &lt;xs:element name="DatabaseName" type="xs:string" /&gt;
        &lt;xs:element name="RootDatabaseName" type="xs:string" /&gt;
      &lt;/xs:sequence&gt;
    &lt;/xs:complexType&gt;
  &lt;/xs:schema&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>XML File (fails)</strong> - I get an Exception when I attempt to deserialize</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Settings
    xmlns="Myco.CLDatabaseBuilder.Models"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="Myco.CLDatabaseBuilder.Models Settings.xsd"&gt;
    &lt;SqlServerInstanceName&gt;SQLEXPRESS&lt;/SqlServerInstanceName&gt;
    &lt;DatabaseName&gt;CL&lt;/DatabaseName&gt;
    &lt;RootDatabaseName&gt;master&lt;/RootDatabaseName&gt;
  &lt;/Settings&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>XML File (succeeds)</strong> - deserialization works fine</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;Settings
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="Myco.CLDatabaseBuilder.Models Settings.xsd"&gt;
    &lt;SqlServerInstanceName&gt;SQLEXPRESS&lt;/SqlServerInstanceName&gt;
    &lt;DatabaseName&gt;CL&lt;/DatabaseName&gt;
    &lt;RootDatabaseName&gt;master&lt;/RootDatabaseName&gt;
  &lt;/Settings&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>Exception Details</strong> - what happens if I run the "failure example"</p>
  
  <pre><code>A first chance exception of type 'System.InvalidOperationException' occurred in System.Xml.dll
  System.InvalidOperationException: There is an error in XML document (2, 2). ---&gt; System.InvalidOperationException: &lt;Settings xmlns='Myco.CLDatabaseBuilder.Models'&gt; was not expected.
     at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderSettings.Read3_Settings()
     --- End of inner exception stack trace ---
     at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
     at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
     at Myco.CLDatabaseBuilder.Models.Settings.Deserialize(String xml) in C:\...\CLDatabaseBuilder\Models\Settings.cs:line 118
     at Myco.CLDatabaseBuilder.Models.Settings.LoadFromFile(String fileName) in C:\...\CLDatabaseBuilder\Models\Settings.cs:line 195
     at Myco.CLDatabaseBuilder.Program.InitializeSettings() in C:\...\CLDatabaseBuilder\Program.cs:line 68
  </code></pre>
  
  <hr>
  
  <p><strong>Questions:</strong></p>
  
  <p>So basically, I have to strip out the default namespace to be able to deserialize.</p>
  
  <p><strong>Any idea why I'm getting an exception when I use <code>xmlns</code>?</strong> This namespace declaration is very helpful for doing live validation in Visual Studio, so I'd like to keep it if possible.</p>
  
  <p><strong>Is something wrong with my XSD root element attributes?</strong> I've messed with <code>elementFormDefault</code> (changing from <code>qualified</code> to <code>unqualified</code>), but this didn't solve the problem.</p>
  
  <hr>
  
  <p><strong>Update</strong></p>
  
  <p>The code it's actually choking on is this:</p>
  
  <pre><code>Serializer = new System.Xml.Serialization.XmlSerializer(typeof(Myco.CDDatabaseBuilder.Models.Settings));
  Serializer.Deserialize(System.Xml.XmlReader.Create(stringReader));
  </code></pre>
  
  <p>As shown in the exception above, the error I'm getting is:</p>
  
  <blockquote>
    <p><code>&lt;Settings xmlns='Myco.CLDatabaseBuilder.Models'&gt;</code> was not expected.</p>
  </blockquote>
  
  <p>Does this suggest anything to look for in my <code>Settings</code> class (the class generated by Xsd2Code)? There are <code>[System.Xml.Serialization...]</code> attributes sprinkled throughout this class. I wonder if something is missing, or if the class (or the serializer) doesn't have a way of modeling the default <code>xmlns</code> attribute. Any ideas appreciated. </p>