Controlling order of serialization in C#

前端 未结 2 1398
抹茶落季
抹茶落季 2020-12-06 04:49

I\'m using an XmlSerializer to serialize an object and write it to a file. I\'ve had quite a bit of success with the serializer doing what I want it to do in terms of nesti

2条回答
  •  半阙折子戏
    2020-12-06 05:39

    [XmlElementAttribute(Order = 1)] 
    public int Field1 {...} 
    
    [XmlElementAttribute(Order = 2)] 
    public int Field2 {...} 
    

    Catch: You must specify the Order for all of your members.

    Be careful - deserialization will only work if the properties in the XML document are in the same order. Otherwise it will silently ignore out-of-order properties.

提交回复
热议问题