I have changed your code a little bit:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace Test_XmlSerialize2
{
public class Program
{
static void Main(string[] args)
{
List<Address> addr = new List<Address>();
Address A1 = new Address();
A1.City = "Chennai";
A1.Number = 1;
addr.Add(A1);
Address A2 = new Address();
A2.City = "Coimbatore";
A2.Number = 2;
addr.Add(A2);
Serialize(addr);
}
private static void Serialize(List<Address> addr)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<Address>), new XmlRootAttribute("AddressDirectory"));
using (TextWriter writer = new StreamWriter(@"XmlSer2.xml", false))
{
serializer.Serialize(writer, addr);
}
}
public static void Serialize(AddressDirectory details)
{
XmlSerializer serializer = new XmlSerializer(typeof(AddressDirectory));
using (TextWriter writer = new StreamWriter(@"XmlSer2.xml", false))
{
serializer.Serialize(writer, details);
}
}
public class AddressDirectory
{
public List<Address> addressobj = new List<Address>();
}
public class Address
{
public int Number { get; set; }
public string City { get; set; }
}
}
}