Search code examples
c#.netxmlserialization.net-7.0

XmlAttribute order or sequence going wrong while serializing .net class


I have a existing class where we have many public properties. Most of them are decorated with [XmlAttribute]. Issue is when I have 16 member then xml show order of attribute correctly

class Market
{
    [XmlArray(Order = 0)]
    [XmlArrayItem(IsNullable = false)]
    public List<Option> Options { get; set; }

    [XmlElement(Order = 1)]
    public MarketType MarketType { get; set; }

    [XmlAttribute]
    public int Id {get;set;}

    [XmlAttribute]
    public string Name {get;set;}

    [XmlAttribute]
    public bool IsEnabled {get;set;}
        .
        .
        .
    // like this 17 XML attribute are there
}
<Market Id="10" Name="Test" IsEnabled="false"  ...  a="" b="" c="" d="">

When I have 17 attribute then it brings 3rd Last and 4th Last attribute in the start and 1st properties goes to 3rd last and 4th last position, i.e. order got disturbed. I tried shuffling element, it only swaps 1st 2 attribute with 3rd and 4th last attribute.

<Market b="" a="" IsEnabled="false"  ... Name="Test" Id="10"  c="" d="">

Solution

  • Got a proper solution, I removed order from XmlArray and XmlElement and it started working fine, Now my class looks like this

    class Market
    {
        [XmlArray]
        [XmlArrayItem(IsNullable = false)]
        public List<Option> Options { get; set; }
    
        [XmlElement]
        public MarketType MarketType { get; set; }
    
        [XmlAttribute]
        public int Id {get;set;}
    
        [XmlAttribute]
        public string Name {get;set;}
    
        [XmlAttribute]
        public bool IsEnabled {get;set;}
        .
        .
        .
        // like this 17 XML attribute are there
    }