0

I have an XML document I need to deserialize where the root element is an array of items, such as

<Items>
  <Item></Item>
  <Item></Item>
</Items>

I have tried creating a class that inherits from a collection such as like...

public class Items : IEnumerable<Item>

but I have not been able to get it to work. I get an error that says, <items> is not expected. I am not even sure it is possible to do what I'm trying to do.

1 Answer 1

2

The following declaration of the Items class works as you need:

[XmlRoot("Items")]
public class Items : List<Item>
{
}

The XmlRootAttribute does the trick, letting the XmlSerializer know about the root element. It then expects elements for items named according to the the Item class.

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

1 Comment

Perfect! Thanks very much.

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.