Home > DeveloperSection > Blogs > Creating XML File in C#

Creating XML File in C#


XML XML 
Ratings:
2 Comment(s)
 3507  View(s)
Rate this:

Creating XML file in C#.

try

            {

                stringfilename = "c:\\firstXML.xml";

//assigning file name of desiredfile we want to create.

                XmlDocumentxmlDoc = new XmlDocument();

//creating new XML document

XmlTextWriter xmlWriter= new     XmlTextWriter(filename, System.Text.Encoding.UTF8);

//creating XmlTestWriter, andpassing file name and encoding type as argument

                xmlWriter.Formatting = Formatting.Indented;

//setting XmlWriter formating tobe indented

xmlWriter.WriteProcessingInstruction("xml", "version='1.0'encoding='UTF-8'");

//writing version and encodingtype of XML in file.

                xmlWriter.WriteStartElement("Employee");

//writing first element

                xmlWriter.Close();

//closing writer

 

                xmlDoc.Load(filename);

//loading XML file

 

                XmlNoderoot = xmlDoc.DocumentElement;

//creating child nodes.

                XmlElementchildNode1 = xmlDoc.CreateElement("ID");

                XmlElementchildNode2 = xmlDoc.CreateElement("Name");

                XmlElementchildNode3 = xmlDoc.CreateElement("Address");

 

//adding child node to root.

                root.AppendChild(childNode1);

                childNode1.InnerText = "1";

//writing inner text of childnode

                root.AppendChild(childNode2);

                childNode2.InnerText = "Alex";

                root.AppendChild(childNode3);

                childNode3.InnerText = "USA";

 

                xmlDoc.Save(filename);

//saving xml file

            }

            catch(Exception ex)

            {

                MessageBox.Show(ex.Message);

           }

nice article

By aken H on   5 years ago

Hi Uttam Misra ,

     this a nice article,if can you provide some  xml language learn then will very good.

thanks!


XML

By Rohit Kesharwani on   5 years ago

Hi aken,

You can easily learn xml language from here:
http://www.w3schools.com/xml/


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

Follow MindStick