views:

72

answers:

3

Hi folks,

I'm trying to xml serialize a POCO view data class into xml. It serializes, but incorrectly generates some xml.

eg. (current result .. not the one I'm after)

<ReviewListViewData>
    <reviews>
        <review>....</review>
        ...
    </reviews>
</ReviewListViewData>

I'm trying to get (notice how I've removed the bad root node?) ...

<reviews>
    <review>....</review>
    ...
</reviews>

Class is defined as...

public class ReviewListViewData
{
    [XmlArray("reviews")]
    [XmlArrayItem("review")]
    public ReviewViewData[] Reviews { get; set; }
}

and here's a sample way it's called in an ASP.NET MVC ActionMethod :-

var reviewListViewData = GetReviewListViewData(...);
return XmlResult(reviewListViewData);  // (XmlResult referenced from MVCContrib).

anyone have any ideas, please?

A: 

Did you try decorating the class ReviewListViewData with [XmlRoot("reviews")] instead of the XmlArray?

ScottE
+6  A: 

Try this:

[XmlRoot("reviews")]
public class ReviewListViewData
{
    [XmlElement("review")]
    public ReviewViewData[] Reviews { get; set; }
}
Darin Dimitrov
Perfect. thanks kindly Darin :)
Pure.Krome
A: 

Check out OXM which is a POCO approach to xml serialization by using a mapping API which gives you an exact control over the generated XML.

Delucia