Reputation: 5899
I have something like the following xml that I need to deserialize (note I cannot change the XML):
<Root>
<Products>
<Product>1</Product>
<Product>2</Product>
<Product>3</Product>
</Products>
</Root>
Here is how I am trying to deserialize it:
[XmlRoot("Root")]
public class ProductsResponse
{
[XmlElement("Products", typeof(MyProduct[]))]
public MyProduct[] Products;
}
The problem is that it will not deserialize because when it gets to Product, it compares that element name to the type of my array, which is MyProduct. Is there any way I can deserialze into a class that is not named Product? I would like to avoid renaming my MyProduct class if possible.
Upvotes: 1
Views: 243
Reputation: 3075
Try using XMLElement: http://msdn.microsoft.com/en-us/library/2baksw0z%28VS.71%29.aspx
Later edit: I was wrong... use XmlArray and XmlArrayItemAttribute
[XmlArray(ElementName = "Products")]
[XmlArrayItem("Product")]
public MyProduct[] Products;
http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlarrayattribute.aspx http://msdn.microsoft.com/en-us/library/2baksw0z%28VS.80%29.aspx
Upvotes: 2