articles

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

How to read and write XML file through C sharp

How to read and write XML file through C sharp

Uttam Misra 53357 01-Aug-2010

How to read and write XML file through C sharp

Writing XML file

Here I’m creating an 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);
            }
            }

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


How to read and write XML file through C sharp



xml xml 
Updated 13-Jul-2020
More than 18 years of working experience in IT sector. We are here to serve you best.

Leave Comment

Comments

Liked By