Nov 28, 2015

Writing XML With The XmlWriter In C#

In This Article Ill Explain About The XML Writer In C#. Lets See First How Our Xml Going To Be.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
<?xml version="1.0" encoding="utf-8"?>
<Students>
   <Student>
      <FirstName>Nifal</FirstName>
      <LastName>Nizar</LastName>
      <Age>25</Age> 
   </Student>
   <Student>
      <FirstName>Dinesh</FirstName>
      <LastName>Selvachandra</LastName>
      <Age>24</Age>
   </Student>
</Students>

I Have Created A Simple Console Application.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
using System;

namespace XML_Writer
{
    class Program
    {
        static void Main(string[] args)
        {
            
        }
    }
}

In Order To Use "XmlWriter" We Need To Import The "System.Xml".
Lets Import And Start Using It.

Now Create An Object Of "XmlWriter" Class With The File Location As You Wish To Keep The Xml File. Its Better To Keep The Keep The File Path In A Variable. Normally In Large Systems It Comes From "Web.Config" File.


1
2
string xmlPath = "D:\\Students.xml";
using (XmlWriter xmlWriter = XmlWriter.Create(xmlPath))

Now Lets Start The Code To Write. First You Should Call The "WriteStartDocument()" Method To Start Writing. 


1
xmlWriter.WriteStartDocument();

According To Our Above Xml We Have Parent Node Call "Students". Then In Side That We Have Two Student Nodes.

To Create A Element Use The "WriteStartElement()" Method And Pass The String Parameter Into Which You Want To Be Your Element.


1
xmlWriter.WriteStartElement("Students");

To Create A Element String Use The "WriteElementString()" Method And Pass Two String Parameters Which Are Going To Be Your Element And Element Value.


1
xmlWriter.WriteElementString("FirstName", "Nifal");

When Your Writing Always Make Sure Your In The Correct Node Element And To Close All The Element Nodes. In Order To Close The Element Node Use The "WriteEndElement()" Method.


1
xmlWriter.WriteEndElement();

Finally Close The Document.


1
xmlWriter.WriteEndDocument();

Lets Have A Look On Full Code Now.


 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
using System;
using System.Xml;

namespace XML_Writer
{
  class Program
    {
      static void Main(string[] args)
      {
        string xmlPath = "D:\\Students.xml";
        using (XmlWriter xmlWriter = XmlWriter.Create(xmlPath))
        {
           xmlWriter.WriteStartDocument();

           xmlWriter.WriteStartElement("Students");

           xmlWriter.WriteStartElement("Student");
           xmlWriter.WriteElementString("FirstName", "Nifal");
           xmlWriter.WriteElementString("LastName", "Nizar");
           xmlWriter.WriteElementString("Age", "25");
           xmlWriter.WriteEndElement();

           xmlWriter.WriteStartElement("Student");
           xmlWriter.WriteElementString("FirstName", "Dinesh");
           xmlWriter.WriteElementString("LastName", "Selvachandra");
           xmlWriter.WriteElementString("Age", "24");
           xmlWriter.WriteEndElement();

           xmlWriter.WriteEndElement();

           xmlWriter.WriteEndDocument();
        }
      }
   }
}

Hope You Enjoyed It.




No comments:

JWT Token Decode Using Jquery

When it come to authentication we use many mechanism. Ones the user authenticated we must keep these details somewhere safe. So we can share...