- Back to Home »
- Windows Applications »
- XMLWriter
Posted by :
Sudhir Chekuri
Thursday, 10 March 2016
XMLWriter class is used to create and xml file.
Below example is used create xml
file in the bin location
using System.Xml;
class Program
{
class Employee
{
int _id;
string _firstName;
string _lastName;
int _salary;
public Employee(int id, string
firstName, string lastName, int salary)
{
this._id = id;
this._firstName = firstName;
this._lastName = lastName;
this._salary = 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; } }
}
static void Main()
{
Employee[] employees = new Employee[4];
employees[0] = new Employee(1, "David", "Smith", 10000);
employees[1] = new Employee(3, "Mark", "Drinkwater", 30000);
employees[2] = new Employee(4, "Norah", "Miller", 20000);
employees[3] = new Employee(12, "Cecil", "Walker", 120000);
using (XmlWriter writer = XmlWriter.Create("employees.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();
}
}
}
XML data
<Employees>
<Employee>
<ID>1</ID>
<FirstName>David</FirstName>
<LastName>Smith</LastName>
<Salary>10000</Salary>
</Employee>
<Employee>
<ID>3</ID>
<FirstName>Mark</FirstName>
<LastName>Drinkwater</LastName>
<Salary>30000</Salary>
</Employee>
<Employee>
<ID>4</ID>
<FirstName>Norah</FirstName>
<LastName>Miller</LastName>
<Salary>20000</Salary>
</Employee>
<Employee>
<ID>12</ID>
<FirstName>Cecil</FirstName>
<LastName>Walker</LastName>
<Salary>120000</Salary>
</Employee>