Whenever we are working with generic lists in C#, sometimes we may require to save this list object to process later. In couple of ways we can implement this, either by using data serialization and store in text file or by saving this list object in XML format. In this article we will discuss about how to save list object in XML format in C#.
Open Microsoft Visual Studio 2013 => Create Console Application, name it as ListToXML. Add simple class Company with three different properties as shown below.
public class Company
{
public int Id { set; get; }
public string Name { set; get; }
public double Salary { set; get; }
}
Now assign some data to this class and add to List as shown below.
List<Company> objList = new List<Company>();
Company obj1 = new Company();
obj1.Id = 1001;
obj1.Name = "A";
obj1.Salary = 5000;
objList.Add(obj1);
Company obj2 = new Company();
obj2.Id = 1002;
obj2.Name = "B";
obj2.Salary = 6000;
objList.Add(obj2);
Company obj3 = new Company();
obj3.Id = 1003;
obj3.Name = "C";
obj3.Salary = 7000;
objList.Add(obj3);
Company obj4 = new Company();
obj4.Id = 1004;
obj4.Name = "D";
obj4.Salary = 8000;
objList.Add(obj4);
We have all data in List object objList of type Company class. Let’s save this list object in XML format by using XmlSerializer & StreamWriter class’s as shown below.
XmlSerializer serialiser = new XmlSerializer(typeof(List<Company>));
TextWriter Filestream = new StreamWriter(@"E:\Company.xml");
serialiser.Serialize(Filestream, objList);
Filestream.Close();
As shown above Company list object objList will save in Company.xml file in E drive. The total code to save List object in XML format is a shown below.
using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;
namespace ListToXML
{
class Program
{
static void Main(string[] args)
{
List<Company> objList = new List<Company>();
Company obj1 = new Company();
obj1.Id = 1001;
obj1.Name = "A";
obj1.Salary = 5000;
objList.Add(obj1);
Company obj2 = new Company();
obj2.Id = 1002;
obj2.Name = "B";
obj2.Salary = 6000;
objList.Add(obj2);
Company obj3 = new Company();
obj3.Id = 1003;
obj3.Name = "C";
obj3.Salary = 7000;
objList.Add(obj3);
Company obj4 = new Company();
obj4.Id = 1004;
obj4.Name = "D";
obj4.Salary = 8000;
objList.Add(obj4);
XmlSerializer serialiser = new XmlSerializer(typeof(List<Company>));
TextWriter Filestream = new StreamWriter(@"E:\Company.xml");
serialiser.Serialize(Filestream, objList);
Filestream.Close();
}
}
public class Company
{
public int Id { set; get; }
public string Name { set; get; }
public double Salary { set; get; }
}
}
Run the application, the output file Company.xml contains list object data in xml format as shown below.
<?xml version="1.0" encoding="utf-8"?>
<ArrayOfCompany xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Company>
<Id>1001</Id>
<Name>A</Name>
<Salary>5000</Salary>
</Company>
<Company>
<Id>1002</Id>
<Name>B</Name>
<Salary>6000</Salary>
</Company>
<Company>
<Id>1003</Id>
<Name>C</Name>
<Salary>7000</Salary>
</Company>
<Company>
<Id>1004</Id>
<Name>D</Name>
<Salary>8000</Salary>
</Company>
</ArrayOfCompany>
If you have any questions, please post it at Question & Answer forum.