Tuesday, August 16, 2011

How to Create a XML File in C#.Net

Following is the program to create a XML File:-

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace CreateXMLFileByManishAgrahari
{


    class Employee
    {
        static void Main(string[] args)
        {
            Employee objEmp = new Employee();
            objEmp.CreateXMLFile();
        }

        protected void CreateXMLFile()
        {
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlWriter writer = XmlWriter.Create("C:\\Manish_Agrahari.xml", settings);
            try
            {
                writer.WriteStartDocument();
                writer.WriteComment("This file is generated by the program.");
                writer.WriteStartElement("Employee");
                writer.WriteElementString("FirstName", "Manish");
                writer.WriteElementString("LastName", "Agrahari");
                writer.WriteEndElement();
                writer.WriteEndDocument();
            }
            catch (Exception ex)
            {
            }
            finally
            {
                writer.Flush();
                writer.Close();
            }
        }

       
    }
}









0 comments: