blog

Home / DeveloperSection / Blogs / Reading XML File in C#

Reading XML File in C#

Uttam Misra4578 20-Dec-2010

Reading from XML file through C#.Net

privatevoid btnRead_Click(objectsender, EventArgs e)

       {

           string doc = "c:\\firstXML.xml";          

           XmlTextReader reader = null;

           // Load thefile with an XmlTextReader

           reader = newXmlTextReader(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.Red;

                   elseif(strCmb1 == "Blue")

                       textBox1.BackColor = Color.Blue;

                   elseif(strCmb1 == "Green")

                       textBox1.BackColor = Color.Green;

                   else

                       textBox1.BackColor = Color.Yellow;

//changing backcolorof 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;

                   elseif(strCmb2 == "Blue")

                       textBox2.BackColor = Color.Blue;

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

                   elseif(strCmb3 == "Blue")

                       textBox3.BackColor = Color.Blue;

                   elseif(strCmb3 == "Green")

                       textBox3.BackColor = Color.Green;

                   else

                       textBox3.BackColor = Color.Yellow;

               }

 

           }

           reader.Close();

           }

               

       }

 


Updated 18-Sep-2014
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By