How to specify the order of XmlAttributes, using XmlSerializer

后端 未结 5 437
轮回少年
轮回少年 2020-12-20 17:49

XmlElement has an \"Order\" attribute which you can use to specify the precise order of your properties (in relation to each other anyway)

相关标签:
5条回答
  • 2020-12-20 18:28

    If you are creating the XML dynamically, try changing the order in which you append the attribute to the node and it should work :)

    0 讨论(0)
  • 2020-12-20 18:37

    From my experience, the order of serialization of attributes is the same as the order you define your public properties. However, if you combine properties with fields in the same class, e.g.

    [Serializable()]
    public class MyClass
    {
       [XmlAttribute("ADoubleProp")]
       public double ADoubleProp { get; set; }
    
       [XmlAttribute("AnIntField")]
       public int AnIntField = 42;
    }
    

    then the fields get written firsts as attributes and then the properties. The code above will produce something like this

    <MyClass AnIntField="42" ADoubleProp="0" />
    
    0 讨论(0)
  • 2020-12-20 18:40

    In C#, as far as what I have found, the order of attributes are serialized in the order that they are defined in the class.

    See my answer to this question here: https://stackoverflow.com/a/21468092/607117

    0 讨论(0)
  • 2020-12-20 18:42

    You don't, as attributes have no order in XML (section 3.1 of the XML recommendation says: "Note that the order of attribute specifications in a start-tag or empty-element tag is not significant.").

    0 讨论(0)
  • 2020-12-20 18:52
    xmlNode.Attributes.InsertAfter(newAttribute, refAttribute); 
    xmlNode.Attributes.InsertBefore(newAttribute, refAttribute);
    
    0 讨论(0)
提交回复
热议问题