---
title: "How to create XML File in asp.net"  
description: "How to create XML File in asp.net"  
author: "Anonymous User"  
published: 2016-01-04  
updated: 2016-01-04  
canonical: https://www.mindstick.com/forum/33831/how-to-create-xml-file-in-asp-dot-net  
category: "asp.net"  
tags: ["asp.net"]  
reading_time: 2 minutes  

---

# How to create XML File in asp.net

I want to use create [XML](https://www.mindstick.com/blog/203/working-with-xml-data-in-sql-server) [File in asp.net](https://www.mindstick.com/forum/158972/how-to-read-appsettings-values-from-a-json-file-in-asp-dot-net-core) .How will do this please help me.

## Replies

### Reply by Anonymous User

a

.NET contains number of classes that support XML. \
ALl of these classes make working with XML as easy as understanding XML.\
i am going to create xml using XmlTextWriter class.

The XmlTextWriter class allows you to write XML to a [file](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp). \
This class contains number of methods and properties.

WriteStartDocument- Writes the XML declaration with the version "1.0".

WriteEndDocument- Closes any open elements.

Close- Closes the stream.

WriteStartElement- Writes the specified start tag.

WriteEndElement- Closes one element.

WriteStartAttribute- Writes the start of an attribute.

WriteEndAttribute- Closes the previous WriteStartAttribute call.

WriteString: Writes a string.\

```

```

## code behind

```
using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Xml;namespace Forumasp{    public partial class XML : System.Web.UI.Page    {        protected void Page_Load(object sender, EventArgs e)        {        }        protected void btnsubmit_Click(object sender, EventArgs e)        {            Employee emp = new Employee();            emp.Date = txtdate.Text;            emp.EmpID = txtempid.Text;            emp.EmpName = txtempname.Text;            emp.Salary = Convert.ToDecimal(txtempsal.Text);            create(emp.EmpID, emp.EmpName, emp.Salary, emp.Date);        }        public void create(string EmpID, string EmpName, decimal Salary,  string Date)        {            XmlTextWriter writer = new XmlTextWriter(Server.MapPath("~/XMLFiles/" + EmpID + ".xml"), System.Text.Encoding.UTF8);                       writer.WriteStartDocument(true);            writer.Formatting = Formatting.Indented;            writer.Indentation = 2;                      writer.WriteStartElement("Employee");                        createNode(EmpID, EmpName, Salary, Date, writer);            writer.WriteEndElement();                      writer.WriteEndDocument();                      writer.Close();        }        private void createNode(string EmpID, string EmpName, decimal Salary, string Date, XmlTextWriter writer)                    {            writer.WriteStartElement("Emp_ID" + EmpID);            writer.WriteStartElement("Emp_ID");            writer.WriteString(EmpID);            writer.WriteEndElement();            writer.WriteStartElement("Emp_Name");            writer.WriteString(EmpName);            writer.WriteEndElement();                      writer.WriteStartElement("Emp_Sal");            writer.WriteString(Salary.ToString());            writer.WriteEndElement();            writer.WriteStartElement("Emp_Date");            writer.WriteString(Date.ToString());            writer.WriteEndElement();                                  writer.WriteEndElement();                              }    }    public class Employee    {              public string EmpID { get; set; }        public string EmpName { get; set; }        public string Date { get; set; }        public decimal Salary { get; set; }    }}
```

## Result

```
<?xml version="1.0" encoding="utf-8" standalone="yes"?><Employee>  <Emp_IDMA10245>    <Emp_ID>MA10245</Emp_ID>    <Emp_Name>Manoj Kumar</Emp_Name>    <Emp_Sal>54214</Emp_Sal>    <Emp_Date>12/12/2015</Emp_Date>  </Emp_IDMA10245></Employee>
```


---

Original Source: https://www.mindstick.com/forum/33831/how-to-create-xml-file-in-asp-dot-net

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
