---
title: "How to read and write XML file through C sharp"  
description: "How to read and write XML file through C sharpWriting XML file  Here I’m creating an XML file through C#.privatevoid btnAccept_Click(object sender,"  
author: "Uttam Misra"  
published: 2010-08-01  
updated: 2020-07-13  
canonical: https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp  
category: "xml"  
tags: ["xml"]  
reading_time: 3 minutes  

---

# How to read and write XML file through C sharp

## How to read and write XML file through C sharp

**Writing XML file**\

Here I’m creating an [XML file](https://www.mindstick.com/articles/198562/xml-vs-html) through C#.

```
private void btnAccept_Click(object sender, EventArgs e)            {         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("ComboBox1");                XmlElement childNode2 = xmlDoc.CreateElement("ComboBox2");                XmlElement childNode3 = xmlDoc.CreateElement("ComboBox3");                 //adding child node to root.                root.AppendChild(childNode1);                childNode1.InnerText = comboBox1.Text;                //assigning innertext of childnode to text of combobox.                root.AppendChild(childNode2);                childNode2.InnerText = comboBox2.Text;                root.AppendChild(childNode3);                childNode3.InnerText = comboBox3.Text;                 xmlDoc.Save(filename);               //saving xml file            }            catch (Exception ex)            {                MessageBox.Show(ex.Message);            }            }
```

![How to read and write XML file through C sharp](https://www.mindstick.com/mindstickarticle/04e767b6-dbb9-4823-bb74-22aa6554b620/images/2fa26332-f277-4d56-906c-16472ca13b5f.png)

![How to read and write XML file through C sharp](https://www.mindstick.com/mindstickarticle/04e767b6-dbb9-4823-bb74-22aa6554b620/images/e53314a7-5b5e-4f4b-b793-a32d6b09f497.png)

##### Reading from XML file through C#.Net

Here I’ll read [from XML file](https://www.mindstick.com/articles/1400/creating-xsd-of-a-xml-using-visual-studio) through C#.Net

```
private void btnRead_Click(object sender, EventArgs e)        {            string doc = "c:\\firstXML.xml";                       XmlTextReader reader = null;                       // Load the file with an XmlTextReader            reader = new XmlTextReader(doc);                        // Read the File            if(File.Exists(doc))            {            while (reader.Read())            {     if (reader.NodeType == XmlNodeType.Element && reader.Name == "ComboBox1")                {//checking where read text is element and and name is “ComboBox1”                    string strCmb1 = reader.ReadElementString();//assigning ReadElementstring to strCmb1.                    if (strCmb1 == "Red")                        textBox1.BackColor = Color.Red;                    else if (strCmb1 == "Blue")                        textBox1.BackColor = Color.Blue;                    else if (strCmb1 == "Green")                        textBox1.BackColor = Color.Green;                    else                        textBox1.BackColor = Color.Yellow;//changing backcolor of text box according to the XML file data.                }    if (reader.NodeType == XmlNodeType.Element && reader.Name == "ComboBox2")                {                    string strCmb2 = reader.ReadElementString();                    if (strCmb2 == "Red")                        textBox2.BackColor = Color.Red;                    else if (strCmb2 == "Blue")                        textBox2.BackColor = Color.Blue;                    else if (strCmb2 == "Green")                        textBox2.BackColor = Color.Green;                    else                        textBox2.BackColor = Color.Yellow;                }    if (reader.NodeType == XmlNodeType.Element && reader.Name == "ComboBox3")                {                    string strCmb3 = reader.ReadElementString();                    if (strCmb3 == "Red")                        textBox3.BackColor = Color.Red;                    else if (strCmb3 == "Blue")                        textBox3.BackColor = Color.Blue;                    else if (strCmb3 == "Green")                        textBox3.BackColor = Color.Green;                    else                        textBox3.BackColor = Color.Yellow;                }             }            reader.Close();            }                        }
```

\

![How to read and write XML file through C sharp](https://www.mindstick.com/mindstickarticle/04e767b6-dbb9-4823-bb74-22aa6554b620/images/f6f7cbd3-226f-4c33-aec5-f2f49f7174e6.png)

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