Writing XML file

Here I’m creating XML file through C#.

private void btnAccept_Click(object sender, EventArgs e)
                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.
//writing first element
//closing writer
//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.
                childNode1.InnerText = comboBox1.Text;
//assigning innertext of childnode to text of combobox.
                childNode2.InnerText = comboBox2.Text;
                childNode3.InnerText = comboBox3.Text;
//saving xml file
            catch (Exception ex)

How to read and write XML file through C sharp

How to read and write XML file through C sharp

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
            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;
                        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;
                        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;
                        textBox3.BackColor = Color.Yellow;

How to read and write XML file through C sharp

  Modified On Nov-30-2017 12:03:54 AM
  1. Thanks for sharing informative post.

    Your words increase my knowledge for sure. Thanks

Leave Comment