Loading...

Using C# to read an write XML in XML File


What is XML?

XML, the short form of Extensible Markup Language is a text-based format that is very flexible and derived from SGML. It is much like HTML but without predefined tags and you have to define your own tags. HTML is about "What data looks" and XML is about "What data is". XML is used to structuring of electronic documents.

XML is becoming famous because it is a very less weighted data Carrier that can be used as a database for small applications.

Example of an XML is given below:

<?xml version="1.0" encoding="utf-8"?>
<QuizData>
    <QuestionData>
       <number>1</number>
       <Question>What is XML</Question>
       <a1>Extensible Markup Language</a1>
       <a2>Extra Markup Language</a2>
       <a3>Extra Storage Markup Language</a3>
       <a4>Extreme Markup Language</a4>
       <correct>Extensible Markup Language</correct>
    </QuestionData> 
<QuizData>

Making XML File using C#


Add these namespaces in the program:


       using System.Data;
      using System.Xml;
        
           using (XmlWriter writer = XmlWriter.Create(name))
                  {
                     writer.WriteStartElement("QuizData");
                     writer.WriteEndElement();                 
                  }


Writing XML in XML File using C#


            XmlDocument xdoc1 = new XmlDocument();
            xdoc1.Load("Quiz.xml"); // Loading XML file
            XmlNode Quiz_data = xdoc1.DocumentElement;

            XmlNode questiondata = xdoc1.CreateElement("QuestionData"); //creating tag

            XmlNode number = xdoc1.CreateElement("number");
            number.InnerText = number.ToString();// writing data to tag “number”
            questiondata.AppendChild(number);

            XmlNode quest = xdoc1.CreateElement("Question");
            quest.InnerText = txt_quest.Text;
            questiondata.AppendChild(quest);

            XmlNode ans1 = xdoc1.CreateElement("a1");
            ans1.InnerText = txt_ans1.Text;
            questiondata.AppendChild(ans1);

            XmlNode ans2 = xdoc1.CreateElement("a2");
            ans2.InnerText = txt_ans2.Text;
            questiondata.AppendChild(ans2);

            XmlNode ans3 = xdoc1.CreateElement("a3");
            ans3.InnerText = txt_ans3.Text;
            questiondata.AppendChild(ans3);

            XmlNode ans4 = xdoc1.CreateElement("a4");
            ans4.InnerText = txt_ans4.Text;
            questiondata.AppendChild(ans4);


            XmlNode correct = xdoc1.CreateElement("correct");
            correct.InnerText = txt_ans1.Text;
            questiondata.AppendChild(correct);   


            xdoc1.DocumentElement.AppendChild(questiondata);
            xdoc1.Save(Quiz.xml); //Saving xml File


Reading XML from XML File using C#


Here I will guide you to read XML from XML file and write it in a datagridview in Windows Form Application using C#. Reading XML and writing XML in a DataGridView in windows Form is very easy. Code is given below:

                DataSet ds = new DataSet();
             ds.ReadXml("Quiz.xml");
             dataGridView1.DataSource = ds.Tables[0].DefaultView;


Deleting specific data from XML File using C#


Deleting any particular data from an XML file using C# seems difficult to some people but it is also an easy task. We have given the code below: 

   string question_number = "1";
   string name = String.Format("//QuestionData[number='" + question_number + "']");  // here we are getting the node having question number “1”


      XmlDocument xdoc1 = new XmlDocument();
      xdoc1.Load("Quiz.xml");
      XmlElement _xmlElement = xdoc1.DocumentElement; // getting main document element



      XmlNode _xmlNode = _xmlElement.SelectSingleNode(name); // getting specific node
      _xmlElement.RemoveChild(_xmlNode);


        xdoc1.Save("Quiz.xml");


Learn How to use DataGridView in Windows Form C# here


Hit Like Button if you understood, It helps a lot to keep us Motivated.

0 comments:

Post a Comment

Google+

 
TOP