Search code examples
c#wcfdatacontractserializer

CollectionDataContractAttribute for list with no group element


My problem is that I would like to use the CollectionDataControlAttribute to deserialise a collection which does not have a group element. The xml looks something like this:

<RootElement>
    <SomeProperty />
    <ListElementEntry />
    <ListElementEntry />
    <ListElementEntry />
    <ListElementEntry />
</RootElement>

Where ListElementEntry appears zero or more times.

Help Appreciated


Solution

  • Does it have to be DataContractSerializer? You can elect to use XmlSerializer for WCF, and then you can use:

    [XmlRoot("RootElement"), XmlType("RootElement")]
    public class Foo {
        public string SomeProperty {get;set;}
        [XmlElement("ListElementEntry")]
        public List<Bar> Bars {get {return bars;}}
        private readonly List<Bar> bars = new List<Bar>();
    }
    

    Ultimately, DataContractSerializer is simply not designed to give you the same level of xml control - which is why it doesn't support even attributes. If you need specific xml, XmlSerializer is usually a better choice.