---
title: "How to create XML file through C Sharp"  
description: "/* Style Definitions */   table.MsoNormalTable  	{mso-style-name:\"Table Normal\";  	mso-tstyle-rowband-size:0;  	mso-tstyle-colband-size:0;  	mso-st"  
author: "Anonymous User"  
published: 2010-08-01  
updated: 2020-03-04  
canonical: https://www.mindstick.com/articles/72/how-to-create-xml-file-through-c-sharp  
category: "xml"  
tags: ["xml"]  
reading_time: 1 minute  

---

# How to create XML file through C Sharp

##### Here I’m creating XML file through C#.

```
          try            {                string filename = "c:\\firstXML.xml";             //assigning file name of desired file we want to create.                XmlDocument xmlDoc = new XmlDocument();             //creating new XML document              XmlTextWriter xmlWriter = new XmlTextWriter(filename, System.Text.Encoding.UTF8);              //creating XmlTestWriter, and passing file name and encoding type as argument                xmlWriter.Formatting = Formatting.Indented;            //setting XmlWriter formating to be indented              xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");            //writing version and encoding type of XML in file.               xmlWriter.WriteStartElement("Employee");             //writing first element                 xmlWriter.Close();             //closing writer                 xmlDoc.Load(filename);              //loading XML file                 XmlNode root = xmlDoc.DocumentElement;              //creating child nodes.                XmlElement childNode1 = xmlDoc.CreateElement("ID");                XmlElement childNode2 = xmlDoc.CreateElement("Name");                XmlElement childNode3 = xmlDoc.CreateElement("Address");                //adding child node to root.                root.AppendChild(childNode1);                childNode1.InnerText = "1";               //writing inner text of child node                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);            }
```

##### Screen Shot

![How to create XML file through C Sharp](https://www.mindstick.com/mindstickarticle/997c6460-d6ab-43c7-9d95-dc00c3737653/images/954b9b81-8e7c-461c-96cc-f8e540fa746c.png)

\

---

Original Source: https://www.mindstick.com/articles/72/how-to-create-xml-file-through-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
