HOW TO CREATE XML FILE IN ASP.NET

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
 530  View(s)
Ratings:
Rate this:
I want to use create XML File in asp.net .How will do this please help me.
  1. aditya kumar Patel

    Post:254

    Points:1810
    Re: How to create XML File in asp.net

    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>

     

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!