Writing XML files in C#
The XmlTextWriter class includes number of methods
to create xml document content very easily. Consider we need to create a xml
file called Employee.xml with the below data,
<?xml version="1.0" encoding="utf-8"?>
<Employees>
<Employee type="Permanent">
<ID>100</ID>
<FirstName>Satheesh</FirstName>
<LastName>Babu</LastName>
<Dept>IT</Dept>
</Employee>
</Employees>
If you see the above xml content, you can visualize it
as an xml document having xml elements (Employees, Employee) or nodes with
attributes (Type) and values (Satheesh). Keeping this in mind, to create the
above XML document programmatically using XmlTextWriter class we need to first
call WriteStartDocument() to start the document creation which emits the xml
declaration. Next, you can subsequently use WriteStartElement(),
WriteAttributeString() method to construct the whole document. Remember, any xml
nodes created should have the matching closing nodes. This is done by calling
WriteEndElement() and WriteEndDocument() methods.
Refer the below code.
protected void Page_Load(object sender, EventArgs e)
{
XmlTextWriter xmlwriter = new
XmlTextWriter(Server.MapPath("App_Data/Employee.xml"), Encoding.UTF8);
xmlwriter.Formatting = Formatting.Indented;
xmlwriter.WriteStartDocument();
xmlwriter.WriteStartElement("Employees");
xmlwriter.WriteStartElement("Employee");
xmlwriter.WriteAttributeString("type",
"Permanent");
xmlwriter.WriteElementString("ID", "100");
xmlwriter.WriteElementString("FirstName",
"Satheesh");
xmlwriter.WriteElementString("LastName",
"Babu");
xmlwriter.WriteElementString("Dept", "IT");
xmlwriter.WriteEndElement();
xmlwriter.WriteEndElement();
xmlwriter.WriteEndDocument();
xmlwriter.Flush();
xmlwriter.Close();
}
Once executed, it will create an xml document called
Employee.xml in your project App_Data folder. You need to include the System.Xml
and System.Text namespace for the above code to work.
In the above example, we have created a very basic xml
document using XmlTextWriter class. This class has many more useful methods
which can be used. The below code creates an xml document from a DataTable
manually including XML comments.
protected void Page_Load(object sender, EventArgs e)
{
XmlTextWriter xmlwriter = new
XmlTextWriter(Server.MapPath("App_Data/Employee.xml"), Encoding.UTF8);
xmlwriter.Formatting = Formatting.Indented;
xmlwriter.WriteStartDocument();
xmlwriter.WriteComment("Programmatically writing
XML");
xmlwriter.WriteStartElement("Employees");
//GetEmployees() method returns DataTable with data.
Download the code attached to see it in action
DataTable dt = GetEmployees();
for (int i = 0; i < dt.Rows.Count; i++)
{
xmlwriter.WriteStartElement("Employee");
xmlwriter.WriteAttributeString("type",
dt.Rows[i]["Type"].ToString());
xmlwriter.WriteElementString("ID",
dt.Rows[i]["ID"].ToString());
xmlwriter.WriteElementString("FirstName",
dt.Rows[i]["FirstName"].ToString());
xmlwriter.WriteElementString("LastName",
dt.Rows[i]["LastName"].ToString());
xmlwriter.WriteElementString("Dept",
dt.Rows[i]["Dept"].ToString());
xmlwriter.WriteEndElement();
}
xmlwriter.WriteEndElement();
xmlwriter.WriteEndDocument();
xmlwriter.Flush();
xmlwriter.Close();
}
|