views:

32

answers:

1

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.

+1  A: 

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

Bogdan
What should I be looking at? The XmlElement that is already present needs to be set to "Products" so it can get to the Products list. Is there a way I can set the element name of the array elements?
Flack
I updated my example to make it clearer
Bogdan
Awesome! It looks like it's working. Thanks a lot.
Flack