title: C# can't deserialize XML containing xsi:type
body: <p>I am trying to deserialize the following XML</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;GovTalkMessage xmlns="http://www.govtalk.gov.uk/CM/envelope"&gt;
    &lt;EnvelopeVersion&gt;2.0&lt;/EnvelopeVersion&gt;
    &lt;Header&gt;
      &lt;MessageDetails&gt;
        &lt;Class&gt;DRUG_DATA&lt;/Class&gt;
        &lt;Qualifier&gt;response&lt;/Qualifier&gt;
        &lt;Function&gt;submit&lt;/Function&gt;
        &lt;CorrelationID&gt;BD694DAAA26AA6068EAAAE5C7746CE54&lt;/CorrelationID&gt;
        &lt;Transformation&gt;XML&lt;/Transformation&gt;
      &lt;/MessageDetails&gt;
      &lt;SenderDetails&gt;
        &lt;IDAuthentication&gt;
          &lt;SenderID /&gt;
          &lt;Authentication&gt;
            &lt;Method /&gt;
            &lt;Role /&gt;
            &lt;Value /&gt;
          &lt;/Authentication&gt;
        &lt;/IDAuthentication&gt;
      &lt;/SenderDetails&gt;
    &lt;/Header&gt;
    &lt;GovTalkDetails&gt;
      &lt;Keys&gt;
        &lt;Key Type="SpokeName" /&gt;
      &lt;/Keys&gt;
    &lt;/GovTalkDetails&gt;
    &lt;Body&gt;
      &lt;S:Envelope xmlns:S="http://www.w3.org/2003/05/soap-envelope"&gt;
        &lt;S:Body&gt;
          &lt;ns2:getGenericDrugsResponse xmlns:ns2="http://webservice.sirkb/"&gt;
            &lt;return&gt;
              &lt;DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug"&gt;
                &lt;GENERIC_DRUG_ID&gt;147&lt;/GENERIC_DRUG_ID&gt;
                &lt;GENERIC_DRUG_NAME&gt;Amoxicilline 125mg/5ml - 60ml&lt;/GENERIC_DRUG_NAME&gt;
              &lt;/DRUG&gt;
              &lt;DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug"&gt;
                &lt;GENERIC_DRUG_ID&gt;149&lt;/GENERIC_DRUG_ID&gt;
                &lt;GENERIC_DRUG_NAME&gt;Amoxicilline 250mg/5ml - 60ml&lt;/GENERIC_DRUG_NAME&gt;
              &lt;/DRUG&gt;            
              &lt;DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug"&gt;
                &lt;DOSAGE&gt;10mg/5ml&lt;/DOSAGE&gt;
                &lt;GENERIC_DRUG_ID&gt;2312&lt;/GENERIC_DRUG_ID&gt;
                &lt;GENERIC_DRUG_NAME&gt;Vinorelbine (as vinorelbine tartrate)&lt;/GENERIC_DRUG_NAME&gt;
                &lt;PHARMACEUTICAL_FORM&gt;concentrate for solution for infusion&lt;/PHARMACEUTICAL_FORM&gt;
              &lt;/DRUG&gt;
              &lt;DRUG xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:genericDrug"&gt;
                &lt;DOSAGE&gt;100 u/ml (approximately 0.58mg)&lt;/DOSAGE&gt;
                &lt;GENERIC_DRUG_ID&gt;2313&lt;/GENERIC_DRUG_ID&gt;
                &lt;GENERIC_DRUG_NAME&gt;Laronidase&lt;/GENERIC_DRUG_NAME&gt;
                &lt;PHARMACEUTICAL_FORM&gt;concentrate for solution for infusion&lt;/PHARMACEUTICAL_FORM&gt;
              &lt;/DRUG&gt;
              &lt;RETURN_STATUS&gt;SUCCESS&lt;/RETURN_STATUS&gt;
            &lt;/return&gt;
          &lt;/ns2:getGenericDrugsResponse&gt;
        &lt;/S:Body&gt;
      &lt;/S:Envelope&gt;
    &lt;/Body&gt;
  &lt;/GovTalkMessage&gt;
  </code></pre>
  
  <p>I have generated the classes through <strong>Paste XML as Classes</strong> feature of Visual Studio 2017. (Using the xsd.exe is the same) I have not changed the generated classes. </p>
  
  <p>They have the following form:</p>
  
  <pre><code>// NOTE: Generated code may require at least .NET Framework 4.5 or .NET Core/Standard 2.0.
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope", IsNullable = false)]
  public partial class GovTalkMessage
  {
  
      private decimal envelopeVersionField;
  
      private GovTalkMessageHeader headerField;
  
      private GovTalkMessageGovTalkDetails govTalkDetailsField;
  
      private GovTalkMessageBody bodyField;
  
      /// &lt;remarks/&gt;
      public decimal EnvelopeVersion
      {
          get
          {
              return this.envelopeVersionField;
          }
          set
          {
              this.envelopeVersionField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public GovTalkMessageHeader Header
      {
          get
          {
              return this.headerField;
          }
          set
          {
              this.headerField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public GovTalkMessageGovTalkDetails GovTalkDetails
      {
          get
          {
              return this.govTalkDetailsField;
          }
          set
          {
              this.govTalkDetailsField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public GovTalkMessageBody Body
      {
          get
          {
              return this.bodyField;
          }
          set
          {
              this.bodyField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageHeader
  {
  
      private GovTalkMessageHeaderMessageDetails messageDetailsField;
  
      private GovTalkMessageHeaderSenderDetails senderDetailsField;
  
      /// &lt;remarks/&gt;
      public GovTalkMessageHeaderMessageDetails MessageDetails
      {
          get
          {
              return this.messageDetailsField;
          }
          set
          {
              this.messageDetailsField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public GovTalkMessageHeaderSenderDetails SenderDetails
      {
          get
          {
              return this.senderDetailsField;
          }
          set
          {
              this.senderDetailsField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageHeaderMessageDetails
  {
  
      private string classField;
  
      private string qualifierField;
  
      private string functionField;
  
      private string correlationIDField;
  
      private string transformationField;
  
      /// &lt;remarks/&gt;
      public string Class
      {
          get
          {
              return this.classField;
          }
          set
          {
              this.classField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string Qualifier
      {
          get
          {
              return this.qualifierField;
          }
          set
          {
              this.qualifierField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string Function
      {
          get
          {
              return this.functionField;
          }
          set
          {
              this.functionField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string CorrelationID
      {
          get
          {
              return this.correlationIDField;
          }
          set
          {
              this.correlationIDField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string Transformation
      {
          get
          {
              return this.transformationField;
          }
          set
          {
              this.transformationField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageHeaderSenderDetails
  {
  
      private GovTalkMessageHeaderSenderDetailsIDAuthentication iDAuthenticationField;
  
      /// &lt;remarks/&gt;
      public GovTalkMessageHeaderSenderDetailsIDAuthentication IDAuthentication
      {
          get
          {
              return this.iDAuthenticationField;
          }
          set
          {
              this.iDAuthenticationField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageHeaderSenderDetailsIDAuthentication
  {
  
      private object senderIDField;
  
      private GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication authenticationField;
  
      /// &lt;remarks/&gt;
      public object SenderID
      {
          get
          {
              return this.senderIDField;
          }
          set
          {
              this.senderIDField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication Authentication
      {
          get
          {
              return this.authenticationField;
          }
          set
          {
              this.authenticationField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageHeaderSenderDetailsIDAuthenticationAuthentication
  {
  
      private object methodField;
  
      private object roleField;
  
      private object valueField;
  
      /// &lt;remarks/&gt;
      public object Method
      {
          get
          {
              return this.methodField;
          }
          set
          {
              this.methodField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public object Role
      {
          get
          {
              return this.roleField;
          }
          set
          {
              this.roleField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public object Value
      {
          get
          {
              return this.valueField;
          }
          set
          {
              this.valueField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageGovTalkDetails
  {
  
      private GovTalkMessageGovTalkDetailsKeys keysField;
  
      /// &lt;remarks/&gt;
      public GovTalkMessageGovTalkDetailsKeys Keys
      {
          get
          {
              return this.keysField;
          }
          set
          {
              this.keysField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageGovTalkDetailsKeys
  {
  
      private GovTalkMessageGovTalkDetailsKeysKey keyField;
  
      /// &lt;remarks/&gt;
      public GovTalkMessageGovTalkDetailsKeysKey Key
      {
          get
          {
              return this.keyField;
          }
          set
          {
              this.keyField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageGovTalkDetailsKeysKey
  {
  
      private string typeField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlAttributeAttribute()]
      public string Type
      {
          get
          {
              return this.typeField;
          }
          set
          {
              this.typeField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class GovTalkMessageBody
  {
  
      private Envelope envelopeField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope")]
      public Envelope Envelope
      {
          get
          {
              return this.envelopeField;
          }
          set
          {
              this.envelopeField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.w3.org/2003/05/soap-envelope", IsNullable = false)]
  public partial class Envelope
  {
  
      private EnvelopeBody bodyField;
  
      /// &lt;remarks/&gt;
      public EnvelopeBody Body
      {
          get
          {
              return this.bodyField;
          }
          set
          {
              this.bodyField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.w3.org/2003/05/soap-envelope")]
  public partial class EnvelopeBody
  {
  
      private getGenericDrugsResponse getGenericDrugsResponseField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://webservice.sirkb/")]
      public getGenericDrugsResponse getGenericDrugsResponse
      {
          get
          {
              return this.getGenericDrugsResponseField;
          }
          set
          {
              this.getGenericDrugsResponseField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://webservice.sirkb/")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://webservice.sirkb/", IsNullable = false)]
  public partial class getGenericDrugsResponse
  {
  
      private @return returnField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
      public @return @return
      {
          get
          {
              return this.returnField;
          }
          set
          {
              this.returnField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.govtalk.gov.uk/CM/envelope", IsNullable = false)]
  public partial class @return
  {
  
      private returnDRUG[] dRUGField;
  
      private string rETURN_STATUSField;
  
      /// &lt;remarks/&gt;
      [System.Xml.Serialization.XmlElementAttribute("DRUG")]
      public returnDRUG[] DRUG
      {
          get
          {
              return this.dRUGField;
          }
          set
          {
              this.dRUGField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string RETURN_STATUS
      {
          get
          {
              return this.rETURN_STATUSField;
          }
          set
          {
              this.rETURN_STATUSField = value;
          }
      }
  }
  
  /// &lt;remarks/&gt;
  [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  public partial class returnDRUG
  {
  
      private string dOSAGEField;
  
      private ushort gENERIC_DRUG_IDField;
  
      private string gENERIC_DRUG_NAMEField;
  
      private string pHARMACEUTICAL_FORMField;
  
      /// &lt;remarks/&gt;
      public string DOSAGE
      {
          get
          {
              return this.dOSAGEField;
          }
          set
          {
              this.dOSAGEField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public ushort GENERIC_DRUG_ID
      {
          get
          {
              return this.gENERIC_DRUG_IDField;
          }
          set
          {
              this.gENERIC_DRUG_IDField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string GENERIC_DRUG_NAME
      {
          get
          {
              return this.gENERIC_DRUG_NAMEField;
          }
          set
          {
              this.gENERIC_DRUG_NAMEField = value;
          }
      }
  
      /// &lt;remarks/&gt;
      public string PHARMACEUTICAL_FORM
      {
          get
          {
              return this.pHARMACEUTICAL_FORMField;
          }
          set
          {
              this.pHARMACEUTICAL_FORMField = value;
          }
      }
  }
  </code></pre>
  
  <p>I get the following error:</p>
  
  <blockquote>
    <p>System.InvalidOperationException: 'There is an error in XML document (33, 14).' </p>
    
    <p>Inner Exception</p>
    
    <p>InvalidOperationException: The specified type was not recognized:
    name='genericDrug', namespace='<a href="http://webservice.sirkb/" rel="nofollow noreferrer">http://webservice.sirkb/</a>', at http://www.govtalk.gov.uk/CM/envelope'>.</p>
  </blockquote>
  
  <p>I have noticed from <a href="https://stackoverflow.com/questions/34067090/automatically-created-c-sharp-classes-for-xml-deserialization-dont-work">this</a> thread that if I remove <code>xsi:type="ns2:genericDrug"</code> from the XML file I can deserialize the XML. I can't modify the XML because it is the response I get for the request. It is not a good practice to do a string replace on XML, so I am looking for a better solution.</p>
  
  <p>This might be a duplicate question from <a href="https://stackoverflow.com/questions/26293499/the-specified-type-was-not-recognized-error-deserializing-xml">this one</a>, but since I am not able to solve the problem I am posting it again because it is difficult to get help in the comment section.</p>
  
  <p>Based on the above question I have tried to change the annotation of <code>public partial class returnDRUG</code> </p>
  
  <p>from</p>
  
  <pre><code>[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.govtalk.gov.uk/CM/envelope")]
  </code></pre>
  
  <p>to</p>
  
  <pre><code>[System.Xml.Serialization.XmlTypeAttribute(Namespace = "http://www.w3.org/2001/XMLSchema-instance", TypeName = "genericDrug")]
  </code></pre>
  
  <p>but still cant parse the XML.</p>
  
  <p>The parsing code is simple</p>
  
  <pre><code>var deserializer = new XmlSerializer(typeof(GovTalkMessage));
  TextReader textReader = new StreamReader("drug.xml"); //saved response in file for simplicity
  GovTalkMessage response;
  response = (GovTalkMessage)deserializer.Deserialize(textReader);
  textReader.Close();
  </code></pre>
  
  <p>What can I do to deserialize the XML in the <code>GovTalkMessage</code> object?</p>