There is a DB class that I want to serialize in xml .

 public class DB { [XmlElement("Nomer")] public string Nomer { get; set; } [XmlElement("Raspolojenie")] public string Raspolojenie { get; set; } [XmlElement("Whois")] public string Whois { get; set; } [XmlElement("Start")] public string Start { get; set; } [XmlElement("End")] public string End { get; set; } } 

I tried this option, but it gives an error.

 List<DB> listDB = new List<DB>(); XmlSerializer xmlSerializer = new XmlSerializer(typeof(DB)); StringWriter stringWriter = new StringWriter(); xmlSerializer.Serialize(stringWriter, listDB); string xml = stringWriter.ToString(); 
  • It seems to me that there are a lot of such questions here. And not necessarily List<T> , any object is serialized according to the same scheme. - nick_n_a

1 answer 1

You declare a serializer with the type of your class :

 new XmlSerializer(typeof(DB)); 

And pass the list of objects of your class:

 xmlSerializer.Serialize(stringWriter, listDB); 

Try to declare a serializer with the list type of your class :

 new XmlSerializer(typeof(List<DB>)); 

Check!