Home > DeveloperSection > Articles > How to read and write XML file through C sharp

How to read and write XML file through C sharp


XML XML 
Ratings:
1 Comment(s)
 36240  View(s)
Rate this:

How to read and write XML file through C#.Net

 Writing XML file

Here I’m creating XML file 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);

            }

            }

Reading from XML file through C#.Net

Here I’ll read from XML file 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();

            }

               

        }


Thanks

By Samuel Fernandes on   one month ago

Thanks for sharing informative post.

Your words increase my knowledge for sure. Thanks


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

Follow MindStick