XMLSerialization of lists without wrapper objects



I have the following xml:



<Templates>
<PreviewList>
<Preview>
<Object>test1</Object>
</Preview>
<Preview>
<Object>test2</Object>
</Preview>
</PreviewList>
</Templates>


I want to turn this into an object, but I do not want a Templates object which holds a previewlist object which holds previews. Instead I want a Templates object which holds a list of Previews. So basicly, keep "previewlist" wrapper in xml, but when deserialized to an object it should not be present.


I tried the following code but the list is always empty. This seems to work in java, so I'm assuming it's just a matter of finding the right configuration for c#.



[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true)]
public class Templates
{

private List<Preview> _previewList = new List<Preview>();

[System.Xml.Serialization.XmlElementAttribute("Preview", IsNullable=false)]
public List<Preview> Previews
{
get{return _previewList;}
set{_previewList = value;}
}

}

[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public class Preview
{
[System.Xml.Serialization.XmlElement(ElementName = "Object")]
public string Object { get; set; }
}

No comments:

Post a Comment