---
title: "Reading XML File in C#"  
description: "Normal0falsefalsefalseEN-USX-NONEX-NONEMicrosoftInternetExplorer4 /* Style Definitions */ table.MsoNormalTable{mso-style-name:\"Table Normal\";mso-tstyl"  
author: "Uttam Misra"  
published: 2010-12-20  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/76/reading-xml-file-in-c-sharp  
category: "xml"  
tags: ["xml"]  
reading_time: 1 minute  

---

# Reading XML File in C#

[Reading](https://www.mindstick.com/articles/126273/books-commonly-found-on-college-reading-lists) from [XML file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) through C#.Net

[private](https://www.mindstick.com/blog/11097/java-access-modifiers-the-public-and-the-private-modifiers)void btnRead_Click(objectsender, EventArgs e)

{

[string](https://www.mindstick.com/articles/1527/string-split-in-c-sharp) doc = "c:\\firstXML.xml";

XmlTextReader reader = null;

// Load thefile 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 readtext is element and name is “ComboBox1”

string strCmb1 = reader.ReadElementString();

//assigningReadElementstring to strCmb1.

if (strCmb1 == "Red")

textBox1.BackColor = [Color](https://www.mindstick.com/articles/77/how-to-split-form-background-color-in-c-sharp).Red;

else if(strCmb1 == "Blue")

textBox1.BackColor = Color.Blue;

else if(strCmb1 == "Green")

textBox1.BackColor = Color.Green;

else

textBox1.BackColor = Color.Yellow;

//changing backcolorof text box according to the XML [file data](https://www.mindstick.com/forum/30/read-xml-file-data-in-c-sharp).

}

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();

}

}

---

Original Source: https://www.mindstick.com/blog/76/reading-xml-file-in-c-sharp

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
