title: C# Deserialize XML to object
body: <p>Having problems deserializing some xml into an object in C#.</p>
  
  <p>The error that I receive is...</p>
  
  <pre><code>xmlns=''&gt; was not expected.
  </code></pre>
  
  <p>The XSD that I received to generate my class is as follows...</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xs:schema targetNamespace="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance" xmlns:pgp="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance" xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified"&gt;
      &lt;xs:element name="ListeAvisRemboursements"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Liste des avis de remboursements&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:complexType&gt;
              &lt;xs:sequence maxOccurs="unbounded"&gt;
                  &lt;xs:element name="AvisRemboursement" type="pgp:AvisRemboursementType"/&gt;
              &lt;/xs:sequence&gt;
          &lt;/xs:complexType&gt;
      &lt;/xs:element&gt;
      &lt;xs:complexType name="AvisRemboursementType"&gt;
          &lt;xs:annotation&gt;
              &lt;xs:documentation&gt;Avis de remboursement lié à une DC&lt;/xs:documentation&gt;
          &lt;/xs:annotation&gt;
          &lt;xs:sequence&gt;
  </code></pre>
  
  <p>(snipped)</p>
  
  <p>The file that I am attempting to import is as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;ListeAvisRemboursements xmlns:ast="xml.AAAAAAA.com/commerce/apres-vente_technique/assistance"&gt;
      &lt;ast:AvisRemboursement NumeroDT="3826961" CodeRA="020545G01" NumeroDC="1"&gt;
          &lt;ast:DateTraitement&gt;2010-06-22&lt;/ast:DateTraitement&gt;
          &lt;ast:MontantDC&gt;25.0&lt;/ast:MontantDC&gt;
          &lt;ast:MontantMO&gt;0.0&lt;/ast:MontantMO&gt;
          &lt;ast:SommeAD&gt;25.0&lt;/ast:SommeAD&gt;
          &lt;ast:MontantPR&gt;0.0&lt;/ast:MontantPR&gt;
          &lt;ast:SommePR&gt;0.0&lt;/ast:SommePR&gt;
          &lt;ast:FraisGestion&gt;0.0&lt;/ast:FraisGestion&gt;
          &lt;ast:NombreHeuresTotalRemboursees&gt;0&lt;/ast:NombreHeuresTotalRemboursees&gt;
          &lt;ast:Etat&gt;C&lt;/ast:Etat&gt;
          &lt;ast:NoteCredit&gt;319984&lt;/ast:NoteCredit&gt;
          &lt;ast:Imputation&gt;030&lt;/ast:Imputation&gt;
          &lt;ast:ListInterventionsPR/&gt;
          &lt;ast:ListInterventionsMO/&gt;
      &lt;/ast:AvisRemboursement&gt;
  </code></pre>
  
  <p>(snipped)</p>
  
  <p>I <em>think</em> what is happening is that when .Net attempts to derserialize the xml, it hits the first line which contains the "xmlns:ast" and complaints about it. As I understand it, .Net will try to map attributes to a public property in the target class (and it wont find one called xmlns. Or there is something wrong with how I am handling the name spaces.</p>
  
  <p>My deserialization code looks as follows:</p>
  
  <pre><code>    XmlDocument _Doc = new XmlDocument();
      _Doc.Load(@"C:\inputfile.xml");
  
      XmlSerializer _XMLSer = new XmlSerializer(typeof(ListeAvisRemboursements));
      ListeAvisRemboursements _X = (ListeAvisRemboursements)_XMLSer.Deserialize(new StringReader(_Doc.OuterXml));
  </code></pre>
  
  <p>I have also tried various combinations of adding a namespace manager to the XML document..</p>
  
  <pre><code>XmlNamespaceManager _Ns = new XmlNamespaceManager(_Doc.NameTable);
  _Ns.AddNamespace("ast", "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance");
  </code></pre>
  
  <p>I understand there is a way I can use this to tell .Net what namespaces to accept.</p>
  
  <p>Would be greatful for some help iwth this problem.</p>
  
  <p>--- Updated at request with class snippet (sorry should have included before) this was created with xsd.exe ---</p>
  
  <pre><code> /// &lt;remarks/&gt;
      [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
      [System.Xml.Serialization.XmlRootAttribute(Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance", IsNullable = false)]
      public partial class ListeAvisRemboursements
      {
  
          private AvisRemboursementType[] avisRemboursementField;
  
          /// &lt;remarks/&gt;
          [System.Xml.Serialization.XmlElementAttribute("AvisRemboursement")]
          public AvisRemboursementType[] AvisRemboursement
          {
              get
              {
                  return this.avisRemboursementField;
              }
              set
              {
                  this.avisRemboursementField = value;
              }
          }
      }
  
      /// &lt;remarks/&gt;
      [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
      [System.SerializableAttribute()]
      [System.Diagnostics.DebuggerStepThroughAttribute()]
      [System.ComponentModel.DesignerCategoryAttribute("code")]
      [System.Xml.Serialization.XmlTypeAttribute(Namespace = "xml.AAAAAAA.com/commerce/apres-vente_technique/assistance")]
      public partial class AvisRemboursementType
      {
  
          private System.DateTime dateTraitementField;
  
          private double montantDCField;
  
          private double montantMOField;
  
          private double sommeADField;
  
          private double montantPRField;
  </code></pre>