简体   繁体   中英

Programmatically controlling the order of xml elements

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.

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:. _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}

XmlSerializer serializer = new XmlSerializer(typeof(totemmessage));

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.

Thanks for reading.

Here are my entity classes:

[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; 

        /// <remarks/> 
        [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; 
            } 
        } 

        /// <remarks/> 
        [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; 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 0)] 
        public energyContractTime period { 
            get { 
                return this.periodField; 
            } 
            set { 
                this.periodField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 1)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool periodSpecified { 
            get { 
                return this.periodFieldSpecified; 
            } 
            set { 
                this.periodFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =2)]        
        public System.DateTime startDate { 
            get { 
                return this.startDateField; 
            } 
            set { 
                this.startDateField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 3)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool startDateSpecified { 
            get { 
                return this.startDateFieldSpecified; 
            } 
            set { 
                this.startDateFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =4)]        
        public System.DateTime endDate { 
            get { 
                return this.endDateField; 
            } 
            set { 
                this.endDateField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 5)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool endDateSpecified { 
            get { 
                return this.endDateFieldSpecified; 
            } 
            set { 
                this.endDateFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElementAttribute(DataType="date", Order =6)]        
        public System.DateTime expiryDate { 
            get { 
                return this.expiryDateField; 
            } 
            set { 
                this.expiryDateField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 7)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool expiryDateSpecified { 
            get { 
                return this.expiryDateFieldSpecified; 
            } 
            set { 
                this.expiryDateFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 8)] 
        public energyInstrumentClassifier type { 
            get { 
                return this.typeField; 
            } 
            set { 
                this.typeField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 9)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool typeSpecified { 
            get { 
                return this.typeFieldSpecified; 
            } 
            set { 
                this.typeFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [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; 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 0)] 
        public decimal price { 
            get { 
                return this.priceField; 
            } 
            set { 
                this.priceField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 1)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool priceSpecified { 
            get { 
                return this.priceFieldSpecified; 
            } 
            set { 
                this.priceFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 2)] 
        public decimal forward { 
            get { 
                return this.forwardField; 
            } 
            set { 
                this.forwardField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 3)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool forwardSpecified { 
            get { 
                return this.forwardFieldSpecified; 
            } 
            set { 
                this.forwardFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 4)] 
        public decimal vol { 
            get { 
                return this.volField; 
            } 
            set { 
                this.volField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 5)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool volSpecified { 
            get { 
                return this.volFieldSpecified; 
            } 
            set { 
                this.volFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 6)] 
        public decimal consensusPrice { 
            get { 
                return this.consensusPriceField; 
            } 
            set { 
                this.consensusPriceField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 7)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool consensusPriceSpecified { 
            get { 
                return this.consensusPriceFieldSpecified; 
            } 
            set { 
                this.consensusPriceFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 8)] 
        public decimal compositePrice { 
            get { 
                return this.compositePriceField; 
            } 
            set { 
                this.compositePriceField = value; 
            } 
        } 

        /// <remarks/>        
        [System.Xml.Serialization.XmlElement(Order = 9)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool compositePriceSpecified { 
            get { 
                return this.compositePriceFieldSpecified; 
            } 
            set { 
                this.compositePriceFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 10)] 
        public decimal reconstitutedForward { 
            get { 
                return this.reconstitutedForwardField; 
            } 
            set { 
                this.reconstitutedForwardField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 11)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool reconstitutedForwardSpecified { 
            get { 
                return this.reconstitutedForwardFieldSpecified; 
            } 
            set { 
                this.reconstitutedForwardFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 12)] 
        public decimal consensusVol { 
            get { 
                return this.consensusVolField; 
            } 
            set { 
                this.consensusVolField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 13)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool consensusVolSpecified { 
            get { 
                return this.consensusVolFieldSpecified; 
            } 
            set { 
                this.consensusVolFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 14)] 
        public decimal compositeVol { 
            get { 
                return this.compositeVolField; 
            } 
            set { 
                this.compositeVolField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 15)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool compositeVolSpecified { 
            get { 
                return this.compositeVolFieldSpecified; 
            } 
            set { 
                this.compositeVolFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 16)] 
        public string priceOut { 
            get { 
                return this.priceOutField; 
            } 
            set { 
                this.priceOutField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 17)] 
        public decimal priceRange { 
            get { 
                return this.priceRangeField; 
            } 
            set { 
                this.priceRangeField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 18)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool priceRangeSpecified { 
            get { 
                return this.priceRangeFieldSpecified; 
            } 
            set { 
                this.priceRangeFieldSpecified = value; 
            } 
        } 

        /// <remarks/>

        [System.Xml.Serialization.XmlElement(Order = 19)] 
        public decimal priceStddev 
        { 
            get { 
                return this.priceStddevField; 
            } 
            set { 
                this.priceStddevField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 20)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool priceStddevSpecified { 
            get { 
                return this.priceStddevFieldSpecified; 
            } 
            set { 
                this.priceStddevFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 21)] 
        public string volOut { 
            get { 
                return this.volOutField; 
            } 
            set { 
                this.volOutField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 22)] 
        public decimal volRange { 
            get { 
                return this.volRangeField; 
            } 
            set { 
                this.volRangeField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 23)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool volRangeSpecified { 
            get { 
                return this.volRangeFieldSpecified; 
            } 
            set { 
                this.volRangeFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 24)] 
        public decimal volStddev { 
            get { 
                return this.volStddevField; 
            } 
            set { 
                this.volStddevField = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElement(Order = 25)] 
        [System.Xml.Serialization.XmlIgnoreAttribute()]        
        public bool volStddevSpecified { 
            get { 
                return this.volStddevFieldSpecified; 
            } 
            set { 
                this.volStddevFieldSpecified = value; 
            } 
        } 

        /// <remarks/> 
        [System.Xml.Serialization.XmlElementAttribute(DataType="integer", Order =26)]        
        public string contributors { 
            get { 
                return this.contributorsField; 
            } 
            set { 
                this.contributorsField = value; 
            } 
        } 
    }

I used XmlElement[Order =n] on top of each of the properties in the entity classes. So, after playing around with the ordering, I could made this serialization work.Inheritance and partial classes made this fix all the more difficult.Thanks.

How to use Order = N together with other XML attributes and List collection.

public class MyClass
{
   [XmlAnyElement("My-XLMComment", Order = 3)] public XmlComment CommentA { get; set; } = new XmlDocument().CreateComment("This is an XML comment");
   [XmlAttribute("MyAttribute")] public string str4 { get; set; } //This is a XML-Attribute, Order not applicable
   [XmlIgnore] public string str1 { get; set; } //This is a [XmlIgnore], Order not applicable
   [XmlElement(Order = 5)] public string str2 { get; set; } // Standard property with Order    
   [XmlElement(Order = 2)] public string str3 { get; set; } // Standard property with Order    
   [XmlElement(Order = 1)] public List<MyList> ListElms = new List<MyList> //List collection with Order
   {
      new ListElm {str1 = "xxx", str2 ="yyy", str3 ="zzz"} ,
      new ListElm {str1 = "xxx", str2 ="yyy", str3 ="zzz"}
   };

   public class MyList
   {
      [XmlElement(Order = 3)] public string str1 { get; set; } // Order can be assigned, but not mandatory 
      [XmlElement(Order = 2)] public string str2 { get; set; } // Order can be assigned, but not mandatory
      [XmlElement(Order = 1)] public string str3 { get; set; } // Order can be assigned, but not mandatory
   }
}

Will serialize to:

<MyClass MyAttribute="str4">
    <ListElms>
       <str3>zzz</str3>
       <str2>yyy</str2>
       <str1>xxx</str1>
    </ListElms>
    <ListElms>
       <str3>zzz</str3>
       <str2>yyy</str2>
       <str1>xxx</str1>
    </ListElms>
       <str3>str3</str3>
       <!--This is an XML comment-->
       <str2>str2</str2>
</MyClass>

The XmlSerializer generates the elements in the order of declaration in the class - can you just change the order in the entity?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM