How do I Set XmlArrayItem Element name for a List implementation?

后端 未结 3 895
星月不相逢
星月不相逢 2020-12-20 12:06

I want to create a custom XML structure as follows:


    

I\'ve created an implementation of <

3条回答
  •  一整个雨季
    2020-12-20 12:51

    I think madd0 shows the simplest option for you here, but just for completeness... personally I don't recommend the "serialize the list as the root object" - for various reasons (including: I've seen those attributes not work on at least on platform - might have been CF or SL, can't remember). Instead, I always advise using a custom root type:

    [XmlRoot("Hotels")]
    public class HotelResult // or something similar
    {
        [XmlElement("Hotel")]
        public List Hotels { get { return hotel; } }
    
        private readonly List hotels = new List();
    }
    

    This will have the same xml structure, and allows greater flexibility (you can add other attributes / elements to the root), and doesn't bake List into your type model (prefer encapsulation over inheritance, etc).

提交回复
热议问题