I have serializable class:
[XmlRoot(ElementName = "News"), XmlType("News")] // I tried many attributes...
public class News
{
[XmlElement("Article")]
public List<Article> Articles { get; set; }
}
And method for serialization:
public static void SerializeToXML(Object obj)
{
XmlSerializer ser = new XmlSerializer(obj.GetType());
...
}
I would like to have the first XML element <News> but it is <ArrayOfArticle>.
Note, I’ve found many similar answers but it seems I have another problem…
If I use ...XmlSerializer(typeof(News)); instead of ...obj.GetType() everything is okay. But there is something wrong with obj.GetType(). It causes that (ser.mapping).ElementName is "ArrayOfArticle". What is the difference?
The only way I’ve been able to find to do this in the past is to create a simple wrapper object which is decorated with the XmlRoot attribute and use it in place of your List.
There could be a more standard way to achieve it but I know this works every time.