using System.Xml.Serialization;
using System.Collections.Generic;
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "ABC", "DEF", 10000);
employees[1] = new Employee(3, "CDE", "EFG", 30000);
employees[2] = new Employee(4, "ABG", "KLM", 20000);
employees[3] = new Employee(12, "MNO", "KJL", 120000);
using (XmlWriter writer = XmlWriter.Create("employeesFirst.xml"))
writer.WriteStartDocument();
writer.WriteStartElement("Employees");
foreach (Employee employee in employees)
writer.WriteStartElement("Employee");
writer.WriteElementString("ID", employee.Id.ToString());
writer.WriteElementString("FirstName", employee.FirstName);
writer.WriteElementString("LastName", employee.LastName);
writer.WriteElementString("Salary", employee.Salary.ToString());
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndDocument();
List<EmployeeXml> employeeXml = new List<EmployeeXml>();
employeeXml.Add(new EmployeeXml {Id =1, FirstName ="ABC", LastName= "DEF",Salary= 10000});
employeeXml.Add(new EmployeeXml {Id =2, FirstName ="CDE", LastName= "FGH",Salary= 60000});
employeeXml.Add(new EmployeeXml {Id =3, FirstName ="HIJ", LastName= "KLM",Salary= 50000});
employeeXml.Add(new EmployeeXml {Id =4, FirstName ="MNO", LastName= "PQR",Salary= 70000});
ExportXmlFile exportXmlFile = new ExportXmlFile();
exportXmlFile.EmployeeXML = employeeXml;
var ns = new XmlSerializerNamespaces();
ns.Add(XmlSchemaDefinitions.NameSpacePrefix, XmlSchemaDefinitions.NameSpace);
var xser = new XmlSerializer(typeof(ExportXmlFile));
using (StringWriter writer = new StringWriter())
xser.Serialize(writer, exportXmlFile,ns);
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new UTF8Encoding(false);
XDocument xmlDoc = XDocument.Parse(writer.ToString());
string _byteOrderMarkUtf8 = Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble());
using (XmlWriter xWriter = XmlWriter.Create("employeesSecond.xml", settings))
public Employee(int id, string firstName, string lastName, int salary)
public int Id { get { return _id; } }
public string FirstName { get { return _firstName; } }
public string LastName { get { return _lastName; } }
public int Salary { get { return _salary; } }
[XmlElement(XmlTagName.ID)]
public int Id { get; set; }
[XmlElement(XmlTagName.FirstName)]
public string FirstName { get; set; }
[XmlElement(XmlTagName.LastName)]
public string LastName { get; set; }
[XmlElement(XmlTagName.Salary)]
public int Salary { get; set; }
public class ExportXmlFile
[XmlArray(XmlTagName.Employees)]
[XmlArrayItem(XmlTagName.Employee)]
public List<EmployeeXml> EmployeeXML { get; set; } = new List<EmployeeXml>();
public static class XmlTagName
public const string Employees = "Employees";
public const string Employee = "Eployee";
public const string ID = "ID";
public const string FirstName = "First Name";
public const string LastName = "Last Name";
public const string Salary = "Salary";
public static class XmlSchemaDefinitions
public static string NameSpacePrefix => "xsi";
public static string NameSpace => "http://www.w3.org/2001/XMLSchema-instance";