0

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?

Community
  • 1
  • 1
Milan Jaros
  • 1,107
  • 17
  • 24

1 Answers1

3

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.

[XmlRoot("Articles")]
public class Articles : List<Article> { }

There could be a more standard way to achieve it but I know this works every time.

M.Babcock
  • 18,753
  • 6
  • 54
  • 84
  • Well, to be honest, I had that wrapper implemented but I passed bad object to my method. Your Answer pointed me to this. Problem was between chair and my keyboard... Thank you and sorry for your time. – Milan Jaros Dec 17 '11 at 03:20