以编程方式控制xml元素的顺序

时间:2011-07-20 09:44:19

标签: c# xml wcf web-services serialization

我在我的.Net应用程序中使用第三方Web服务[WS]。 我的应用程序使用从这个WS的wsdl生成的实体类。 我从db获取数据,将其填入实体对象,然后使用XmlSerializer类从中生成一个xml。

WS的一个方法需要这个xml字符串作为输入。并且xml应该具有与WS所期望的相同顺序的元素。但是发生的事情是某些元素在我的应用程序中颠倒了所以WS在序列化方面抛出了一个内心的例外: _innerException {“不一致的排序:如果在类的一个成员上使用,所有类似粒子的成员都需要'Order'属性,请使用类成员上的XmlElement,XmlAnyElement或XmlArray自定义属性显式设置'Order' instrument'。“} System.Exception {System.InvalidOperationException}

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

所以,我的问题是,如何在将应用程序发送到WS之前以编程方式控制我的应用程序中这些xml元素的顺序?注意:我不想为此目的使用xslt。

感谢阅读。

以下是我的实体类:

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

3 个答案:

答案 0 :(得分:3)

我在实体类的每个属性的顶部使用了XmlElement [Order = n]。因此,在完成订购之后,我可以使这个序列化工作。继承和部分类使得这个修复变得更加困难!谢谢。

答案 1 :(得分:0)

XmlSerializer按类中声明的顺序生成元素 - 你能改变实体中的顺序吗?

答案 2 :(得分:0)

如何与其他XML属性和列表集合一起使用Order = N

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
   }
}

将序列化为:

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