XML FILE CREATION USING C# or ASP.NET

                                                           Home                                               Next

XML FILE CREATION USING C# or ASP.NET

We can create the XML file using C#.NET.  XML files are useful for storing particular information. We can transfer this XML files from one environment to other environment very easily.

In the below program i am creating the one XML file using c# language. We can create this XML file using XmlTextWriter class. 

Output: 

XML FILE CREATION USING C# or ASP.NET

XML FILE CREATION USING C# or ASP.NET

Program:

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

namespace XMLFileCreation
{
    class Program
    {
        static void Main(string[] args)
        {
            string filePath = @"c:/Customer.xml";
            //it is used to create a xml file
            XmlTextWriter objWriter = new XmlTextWriter(filePath, System.Text.Encoding.UTF8);
            //writes the starts of an a
            //Writes the XML declarion with the version1.0 and the standalone attribute
            objWriter.WriteStartDocument(true);
            //How the output is formatted(child elements should be indented properly). 
            objWriter.Formatting = Formatting.Indented;
            //sets how many indentation characters are required for identification of every row. 
            objWriter.Indentation = 4;
            //Writes the first node
            objWriter.WriteStartElement("Customer_Details");
            createNode("1111", "Ranga Rajesh", objWriter);
            createNode("1112", "Anil Kumar", objWriter);
            createNode("1113", "Murthy", objWriter);
            createNode("1114", "Sanjeev", objWriter);
            //close the corresponding end element. 
            objWriter.WriteEndElement();
            //close the corresponding document.
            objWriter.WriteEndDocument();
            //closing the xml writer object. 
            objWriter.Close();
            Console.WriteLine("xml file created");
        }
        public static void createNode(string CustId, string CustName, XmlTextWriter writer)
        {
            writer.WriteStartElement("Customer");
            writer.WriteStartElement("CustomerId");
            writer.WriteString(CustId);
            writer.WriteEndElement();
            writer.WriteStartElement("CustomerName");
            writer.WriteString(CustName);
            writer.WriteEndElement();
            writer.WriteEndElement();
        }
    }
}


                                                           Home                                               Next

Share this post :

Post a Comment

Please give your valuable feedback on this post. You can submit any ASP.NET article here. We will post that article in this website by your name.

 
Support : Ranga Rajesh Kumar
Copyright © 2012. ASP.NET Examples - All Rights Reserved
Site Designed by Ranga Rajesh Kumar