title: Programmatically controlling the order of xml elements
body: <p>I'm using a third part web service[WS] in my .Net app.
  My application is using the entity classes generated from this WS's wsdl.
  I fetch the data from db, fill it in entity objects and then generate an xml out of it using XmlSerializer class.</p>
  
  <p>One of the methods of the WS requires this xml string as input.And the xml should have elements in the same order as expected by the WS.But whats happening is that some of the elements are getting upside down in my app and so WS is throwing an innermost exception saying on serialzation:.
  <strong>_innerException {"Inconsistent sequencing: if used on one of the class's members, the 'Order' property is required on all particle-like members, please explicitly set 'Order' using XmlElement, XmlAnyElement or XmlArray custom attribute on class member 'instrument'."}     System.Exception {System.InvalidOperationException}</strong></p>
  
  <pre><code>XmlSerializer serializer = new XmlSerializer(typeof(totemmessage));
  </code></pre>
  
  <p>So,my question here is, how do I programmatically control the order of these xml elements in my app before sending it to the WS?Note:I dont want to use xslt for this purpose.</p>
  
  <p>Thanks for reading.</p>
  
  <p>Here are my entity classes:</p>
  
  <pre><code>[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
      [System.SerializableAttribute()] 
      [System.Diagnostics.DebuggerStepThroughAttribute()] 
      [System.ComponentModel.DesignerCategoryAttribute("code")] 
      [System.Xml.Serialization.XmlTypeAttribute(TypeName="totem-message")] 
      [System.Xml.Serialization.XmlRootAttribute("totem", Namespace="", IsNullable=false)] 
      public partial class totemmessage  { 
  
          private object itemField; 
  
          private ItemChoiceType1 itemElementNameField; 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElementAttribute("error", typeof(errorinfo))] 
          [System.Xml.Serialization.XmlElementAttribute("parseReport", typeof(parseReportinfo))] 
          [System.Xml.Serialization.XmlElementAttribute("results", typeof(templateinfo))] 
          [System.Xml.Serialization.XmlElementAttribute("subareas", typeof(subareasinfo))] 
          [System.Xml.Serialization.XmlElementAttribute("template", typeof(templateinfo))] 
          [System.Xml.Serialization.XmlElementAttribute("upload", typeof(templateinfo))] 
          [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")] 
          [System.Xml.Serialization.XmlElement(Order = 0)]       
          public object Item { 
              get { 
                  return this.itemField; 
              } 
              set { 
                  this.itemField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 1)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()] 
          public ItemChoiceType1 ItemElementName { 
              get { 
                  return this.itemElementNameField; 
              } 
              set { 
                  this.itemElementNameField = value; 
              } 
          }        
  
      }
  
  [System.Xml.Serialization.XmlIncludeAttribute(typeof(energyInstrument))] 
      [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
      [System.SerializableAttribute()] 
      [System.Diagnostics.DebuggerStepThroughAttribute()] 
      [System.ComponentModel.DesignerCategoryAttribute("code")] 
      public abstract partial class abstractEnergyInstrument { 
  
          private energyContractTime periodField; 
  
          private bool periodFieldSpecified; 
  
          private System.DateTime startDateField; 
  
          private bool startDateFieldSpecified; 
  
          private System.DateTime endDateField; 
  
          private bool endDateFieldSpecified; 
  
          private System.DateTime expiryDateField; 
  
          private bool expiryDateFieldSpecified; 
  
          private energyInstrumentClassifier typeField; 
  
          private bool typeFieldSpecified; 
  
          private string strikeField; 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 0)] 
          public energyContractTime period { 
              get { 
                  return this.periodField; 
              } 
              set { 
                  this.periodField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 1)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool periodSpecified { 
              get { 
                  return this.periodFieldSpecified; 
              } 
              set { 
                  this.periodFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =2)]        
          public System.DateTime startDate { 
              get { 
                  return this.startDateField; 
              } 
              set { 
                  this.startDateField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 3)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool startDateSpecified { 
              get { 
                  return this.startDateFieldSpecified; 
              } 
              set { 
                  this.startDateFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =4)]        
          public System.DateTime endDate { 
              get { 
                  return this.endDateField; 
              } 
              set { 
                  this.endDateField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 5)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool endDateSpecified { 
              get { 
                  return this.endDateFieldSpecified; 
              } 
              set { 
                  this.endDateFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =6)]        
          public System.DateTime expiryDate { 
              get { 
                  return this.expiryDateField; 
              } 
              set { 
                  this.expiryDateField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 7)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool expiryDateSpecified { 
              get { 
                  return this.expiryDateFieldSpecified; 
              } 
              set { 
                  this.expiryDateFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 8)] 
          public energyInstrumentClassifier type { 
              get { 
                  return this.typeField; 
              } 
              set { 
                  this.typeField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 9)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool typeSpecified { 
              get { 
                  return this.typeFieldSpecified; 
              } 
              set { 
                  this.typeFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 10)] 
          public string strike { 
              get { 
                  return this.strikeField; 
              } 
              set { 
                  this.strikeField = value; 
              } 
          } 
      }
  
  [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")] 
      [System.SerializableAttribute()] 
      [System.Diagnostics.DebuggerStepThroughAttribute()] 
      [System.ComponentModel.DesignerCategoryAttribute("code")] 
      [System.Xml.Serialization.XmlRootAttribute("instrument", Namespace="", IsNullable=false)] 
      public partial class energyInstrument : abstractEnergyInstrument { 
  
          private decimal priceField; 
  
          private bool priceFieldSpecified; 
  
          private decimal forwardField; 
  
          private bool forwardFieldSpecified; 
  
          private decimal volField; 
  
          private bool volFieldSpecified; 
  
          private decimal consensusPriceField; 
  
          private bool consensusPriceFieldSpecified; 
  
          private decimal compositePriceField; 
  
          private bool compositePriceFieldSpecified; 
  
          private decimal reconstitutedForwardField; 
  
          private bool reconstitutedForwardFieldSpecified; 
  
          private decimal consensusVolField; 
  
          private bool consensusVolFieldSpecified; 
  
          private decimal compositeVolField; 
  
          private bool compositeVolFieldSpecified; 
  
          private string priceOutField; 
  
          private decimal priceRangeField; 
  
          private bool priceRangeFieldSpecified; 
  
          private decimal priceStddevField; 
  
          private bool priceStddevFieldSpecified; 
  
          private string volOutField; 
  
          private decimal volRangeField; 
  
          private bool volRangeFieldSpecified; 
  
          private decimal volStddevField; 
  
          private bool volStddevFieldSpecified; 
  
          private string contributorsField; 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 0)] 
          public decimal price { 
              get { 
                  return this.priceField; 
              } 
              set { 
                  this.priceField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 1)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool priceSpecified { 
              get { 
                  return this.priceFieldSpecified; 
              } 
              set { 
                  this.priceFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 2)] 
          public decimal forward { 
              get { 
                  return this.forwardField; 
              } 
              set { 
                  this.forwardField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 3)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool forwardSpecified { 
              get { 
                  return this.forwardFieldSpecified; 
              } 
              set { 
                  this.forwardFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 4)] 
          public decimal vol { 
              get { 
                  return this.volField; 
              } 
              set { 
                  this.volField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 5)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool volSpecified { 
              get { 
                  return this.volFieldSpecified; 
              } 
              set { 
                  this.volFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 6)] 
          public decimal consensusPrice { 
              get { 
                  return this.consensusPriceField; 
              } 
              set { 
                  this.consensusPriceField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 7)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool consensusPriceSpecified { 
              get { 
                  return this.consensusPriceFieldSpecified; 
              } 
              set { 
                  this.consensusPriceFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 8)] 
          public decimal compositePrice { 
              get { 
                  return this.compositePriceField; 
              } 
              set { 
                  this.compositePriceField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt;        
          [System.Xml.Serialization.XmlElement(Order = 9)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool compositePriceSpecified { 
              get { 
                  return this.compositePriceFieldSpecified; 
              } 
              set { 
                  this.compositePriceFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 10)] 
          public decimal reconstitutedForward { 
              get { 
                  return this.reconstitutedForwardField; 
              } 
              set { 
                  this.reconstitutedForwardField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 11)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool reconstitutedForwardSpecified { 
              get { 
                  return this.reconstitutedForwardFieldSpecified; 
              } 
              set { 
                  this.reconstitutedForwardFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 12)] 
          public decimal consensusVol { 
              get { 
                  return this.consensusVolField; 
              } 
              set { 
                  this.consensusVolField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 13)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool consensusVolSpecified { 
              get { 
                  return this.consensusVolFieldSpecified; 
              } 
              set { 
                  this.consensusVolFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 14)] 
          public decimal compositeVol { 
              get { 
                  return this.compositeVolField; 
              } 
              set { 
                  this.compositeVolField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 15)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool compositeVolSpecified { 
              get { 
                  return this.compositeVolFieldSpecified; 
              } 
              set { 
                  this.compositeVolFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 16)] 
          public string priceOut { 
              get { 
                  return this.priceOutField; 
              } 
              set { 
                  this.priceOutField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 17)] 
          public decimal priceRange { 
              get { 
                  return this.priceRangeField; 
              } 
              set { 
                  this.priceRangeField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 18)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool priceRangeSpecified { 
              get { 
                  return this.priceRangeFieldSpecified; 
              } 
              set { 
                  this.priceRangeFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt;
  
          [System.Xml.Serialization.XmlElement(Order = 19)] 
          public decimal priceStddev 
          { 
              get { 
                  return this.priceStddevField; 
              } 
              set { 
                  this.priceStddevField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 20)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool priceStddevSpecified { 
              get { 
                  return this.priceStddevFieldSpecified; 
              } 
              set { 
                  this.priceStddevFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 21)] 
          public string volOut { 
              get { 
                  return this.volOutField; 
              } 
              set { 
                  this.volOutField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 22)] 
          public decimal volRange { 
              get { 
                  return this.volRangeField; 
              } 
              set { 
                  this.volRangeField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 23)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool volRangeSpecified { 
              get { 
                  return this.volRangeFieldSpecified; 
              } 
              set { 
                  this.volRangeFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 24)] 
          public decimal volStddev { 
              get { 
                  return this.volStddevField; 
              } 
              set { 
                  this.volStddevField = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElement(Order = 25)] 
          [System.Xml.Serialization.XmlIgnoreAttribute()]        
          public bool volStddevSpecified { 
              get { 
                  return this.volStddevFieldSpecified; 
              } 
              set { 
                  this.volStddevFieldSpecified = value; 
              } 
          } 
  
          /// &lt;remarks/&gt; 
          [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order =26)]        
          public string contributors { 
              get { 
                  return this.contributorsField; 
              } 
              set { 
                  this.contributorsField = value; 
              } 
          } 
      }
  </code></pre>