title: Contract-First XML attributes (dis)order
body: <p>I'm having a strange case. I'm using the <a href="https://msdn.microsoft.com/en-us/library/hh674270(v=vs.110).aspx" rel="nofollow">contract-first approach</a> to develop a WCF Web Service. The thing is I have defined a specific attribute order in the XSD file, with the  tag. Nevertheless the Generated code with the contract-first tool is messing this order, and when I try to send a xml with the specified attribute distribution I'm getting this error (using the SoapUI application):</p>
  
  <p>Element' 'codigoProvinciaField' from namespace '<a href="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes" rel="nofollow">http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes</a>' is not expected. Expecting element 'codigoCorporacionField'
  (translated from the message below)</p>
  
  <p>It works when I swap the "codigoCorporacionField" with the "codigoProvinciaField". The thing is I've them defined the other way around in the xml schema.</p>
  
  <p>It seems that without the contract-first approach I can use the <a href="https://msdn.microsoft.com/en-us/library/ms729813%28v=vs.110%29.aspx?f=255&amp;MSPPError=-2147217396" rel="nofollow">[DataMember(Order = 0)] tag</a>. </p>
  
  <p>The message:</p>
  
  <blockquote>
    <p>'<a href="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes" rel="nofollow">http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes</a>'.
    Se espera el elemento 'codigoCorporacionField'.
     Error en la línea 9, posición 42. No se esperaban los
    elementos 'Element' 'codigoProvinciaField' del espacio de nombres
    '<a href="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes" rel="nofollow">http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes</a>'.
    Se espera el elemento 'codigoCorporacionField'.
                      en System.Runtime.Serialization.XmlObjectSerializerReadContext.ThrowRequiredMemberMissingException(XmlReaderDelegator
    xmlReader, Int32 memberIndex, Int32 requiredIndex,
    XmlDictionaryString[] memberNames)    en
    System.Runtime.Serialization.XmlObjectSerializerReadContext.GetMemberIndexWithRequiredMembers(XmlReaderDelegator
    xmlReader, XmlDictionaryString[] memberNames, XmlDictionaryString[]
    memberNamespaces, Int32 memberIndex, Int32 requiredIndex,
    ExtensionDataObject extensionData)    en
    ReadCodigoEnteFromXml(XmlReaderDelegator ,
    XmlObjectSerializerReadContext , XmlDictionaryString[] ,
    XmlDictionaryString[] )    en
    System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator
    xmlReader, XmlObjectSerializerReadContext context)    en
    System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
    reader, String name, String ns, Type declaredType, DataContract&amp;
    dataContract)    en
    System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
    xmlReader, Int32 id, RuntimeTypeHandle declaredTypeHandle, String
    name, String ns)    en ReadEnvioFromXml(XmlReaderDelegator ,
    XmlObjectSerializerReadContext , XmlDictionaryString[] ,
    XmlDictionaryString[] )    en
    System.Runtime.Serialization.ClassDataContract.ReadXmlValue(XmlReaderDelegator
    xmlReader, XmlObjectSerializerReadContext context)    en
    System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
    reader, String name, String ns, Type declaredType, DataContract&amp;
    dataContract)    en
    System.Runtime.Serialization.XmlObjectSerializerReadContext.InternalDeserialize(XmlReaderDelegator
    xmlReader, Type declaredType, DataContract dataContract, String name,
    String ns)    en
    System.Runtime.Serialization.DataContractSerializer.InternalReadObject(XmlReaderDelegator
    xmlReader, Boolean verifyObjectName, DataContractResolver
    dataContractResolver)    en
    System.Runtime.Serialization.XmlObjectSerializer.ReadObjectHandleExceptions(XmlReaderDelegator
    reader, Boolean verifyObjectName, DataContractResolver
    dataContractResolver)    en
    System.Runtime.Serialization.DataContractSerializer.ReadObject(XmlDictionaryReader
    reader, Boolean verifyObjectName)    en
    System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.PartInfo.ReadObject(XmlDictionaryReader
    reader, XmlObjectSerializer serializer)    en
    System.ServiceModel.Dispatcher.DataContractSerializerOperationFormatter.DeserializeParameterPart(XmlDictionaryReader
    reader, PartInfo part, Boolean isRequest)
                      System.Runtime.Serialization.SerializationException
                   
                   El formateador inició una excepción al intentar deserializar el mensaje: Error al intentar deserializar el
    parámetro <a href="http://tempuri.org/:envio" rel="nofollow">http://tempuri.org/:envio</a>. El mensaje de InnerException era
    'Error en la línea 9, posición 42. No se esperaban los elementos
    'Element' 'codigoProvinciaField' del espacio de nombres
    '<a href="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes" rel="nofollow">http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes</a>'.
    Se espera el elemento 'codigoCorporacionField'.'. Consulte
    InnerException para obtener más información.</p>
  </blockquote>
  
  <p>The XSD file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"&gt;
  
    &lt;xsd:element name="Envio"&gt;
      &lt;xsd:complexType&gt;
        &lt;xsd:sequence&gt;
          &lt;xsd:element name="Version" type="xsd:string"&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element ref="CodigoEnte" minOccurs="1" maxOccurs="1"/&gt;
        &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
    &lt;/xsd:element&gt; 
  
    &lt;xsd:element name="CodigoEnte"&gt;
      &lt;xsd:complexType&gt;
        &lt;xsd:sequence&gt;
          &lt;xsd:element name="CodigoComunidad"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="2"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="CodigoProvincia"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="2"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="CodigoCorporacion"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="3"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="Tiporg1"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="1"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="Tiporg2"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="1"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
          &lt;xsd:element name="Tiporg3"&gt;
            &lt;xsd:simpleType&gt;
              &lt;xsd:restriction base="xsd:string"&gt;
                &lt;xsd:length value="3"/&gt;
              &lt;/xsd:restriction&gt;
            &lt;/xsd:simpleType&gt;
          &lt;/xsd:element&gt;
        &lt;/xsd:sequence&gt;
      &lt;/xsd:complexType&gt;
    &lt;/xsd:element&gt;
  
  &lt;/xsd:schema&gt;
  </code></pre>
  
  <p>And the XML sent using SoapUI:</p>
  
  <pre><code>&lt;soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:tem="http://tempuri.org/" xmlns:inv="http://schemas.datacontract.org/2004/07/InventarioWS.ContractTypes"&gt;
     &lt;soapenv:Header/&gt;
     &lt;soapenv:Body&gt;
        &lt;tem:RecuperaCorporacionXCodigo&gt;
           &lt;!--Optional:--&gt;
           &lt;tem:envio&gt;
              &lt;inv:codigoEnteField&gt;
                 &lt;inv:codigoComunidadField&gt;12&lt;/inv:codigoComunidadField&gt;
                 &lt;inv:codigoProvinciaField&gt;28&lt;/inv:codigoProvinciaField&gt;
                 &lt;inv:codigoCorporacionField&gt;022&lt;/inv:codigoCorporacionField&gt;
                 &lt;inv:tiporg1Field&gt;A&lt;/inv:tiporg1Field&gt;
                 &lt;inv:tiporg2Field&gt;A&lt;/inv:tiporg2Field&gt;
                 &lt;inv:tiporg3Field&gt;000&lt;/inv:tiporg3Field&gt;
              &lt;/inv:codigoEnteField&gt;
              &lt;inv:versionField&gt;?&lt;/inv:versionField&gt;
           &lt;/tem:envio&gt;
        &lt;/tem:RecuperaCorporacionXCodigo&gt;
     &lt;/soapenv:Body&gt;
  &lt;/soapenv:Envelope&gt;
  </code></pre>
  
  <p>Edit:  I forgot to say the error comes from an unsolicicited alfabetical order given by the framework/tool. In this <a href="https://social.msdn.microsoft.com/Forums/silverlight/en-US/12ab002d-f49f-4e25-9adb-ddbb58174efe/error-while-trying-to-deserialize-parameter?forum=silverlightnet" rel="nofollow">answer</a> says:</p>
  
  <blockquote>
    <p>By specifying the Order property in this way, RegionID will come before RegionDescription in the generated schema. Without this property, the sequence in the schema is sorted alphabetically. Visual Studio will generate the correct proxy, but I don't know how your own class looks like. Make sure your client side specify the same Order property.</p>
  </blockquote>