5

I need Xml that looks like this

<foo>
  <bar ... />
  <bar ... />
</foo>

And currently have the following class structure :

[XmlRoot("foo")]
public class Foo
{
  [XmlArrayItem("bar")]
  public List<Bar> myBars;
}

But this gives me Xml where bar items are wrapped inside a bars element. How should I define my custom XmlAttributes so I'd get the Xml structure I need?

2 Answers 2

7

I had to solve something similar yesterday, and this was the solution for me:

[XmlRoot("foo")]
public class Foo
{
    [XmlElement("bar")]
    public List<Bar> myBars;
}
Sign up to request clarification or add additional context in comments.

Comments

3

The solution I use is this:

[XmlRoot("foo")]
public class Foo : List<Bar>
{
}

[XmlType("bar")]
public class Bar
{
}

In fact, I defined Foo as a List<T>, so it works as a generic list. The type in that list just needs to define the XmlType attribute.

1 Comment

Can have only one right answer :( I would've liked to mark both as correct answers.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.