2

I have

public class Convolutions : List<Convolution> { }

Which serializes as xml:

<ArrayOfConvolution>
     <Convolution>

But I would like:

<Convolutions>
    <Convolution>

But it is not possible to use [XmlArray("Convolutions")] or [XmlElement(ElementName = "Convolutions")] on a class.
Anyway to acheive this?

2 Answers 2

3

[XmlType("Convolutions")]
public class Convolutions : List<Convolution>{ }

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks - that works - probably XmlRoot more approriate - but really 6 or one half a dozen of the other...
XmlRoot only works when it's the root. XmlType works everytime.
1

If that is the root object, then:

[XmlRoot("Convolutions")]
public class Convolutions : List<Convolution> { }

If it is a member of another object, use attributes on the member.

Another approach, more flexible in many cases, is to use a separate wrapper object and encapsulate the list; frankly, inheriting from lists is not usually very helpful. The following would work:

public class Convolutions {
    [XmlElement("Convolution")]
    public List<Convolution> Items { get; set; }
}

or if you don't like set on your collection members:

public class Convolutions {
    private readonly List<Convolution> items = new List<Convolution>();
    [XmlElement("Convolution")]
    public List<Convolution> Items { get { return items; } }
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.