Home > DeveloperSection > Forums > How to create XML File in asp.net
Ankit Singh

Total Post:341

Points:2389
Posted on    January-04-2016 10:03 PM

 ASP.Net ASP.Net 
Ratings:


 1 Reply(s)
 228  View(s)
Rate this:
I want to use create XML File in asp.net .How will do this please help me.


aditya kumar Patel

Total Post:246

Points:1752
Posted on    January-04-2016 10:17 PM

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.
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>

 


Don't want to miss updates? Please click the below button!

Follow MindStick