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

Reading XML File in C#


XML XML 
Ratings:
0 Comment(s)
 2146  View(s)
Rate this:

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 = 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.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.

               }

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

           }

               

       }

 


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

Follow MindStick