Demystify XmlDocument Class
According
to msdn documentation below is the class hierarchy of XmlTextWriter class.
System.Object
System.Xml.XmlWriter
System.Xml.XmlTextWriter
Class visibility scope
is public
public class XmlTextWriter : XmlWriter
And from class definition
it is clear that the class is derive from XmlWriter class. Like XMLdocument class it also contain three
constructor and those are
XmlTextWriter(TextWriter)
XmlTextWriter(Stream,
Encoding)
XmlTextWriter(String,
Encoding)
Few property of XmlTextWriter
are
BaseStream
Formatting
IndentChar
Setting
And many more
Few Methods are
WriteDocType
WriteComment
WriteEndElement
And
many more
Let’s
see one example to create XML document using XmlTextWriter Class.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Data;
using System.Xml;
using System.IO;
namespace BlogProject
{
class Program
{
static void Main(string[]
args)
{
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
xmlTextWriter.Formatting = Formatting.Indented;
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("Persons");
xmlTextWriter.WriteStartElement("Person");
xmlTextWriter.WriteElementString("Name", "Sourav");
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
XmlDocument
docSave = new XmlDocument();
docSave.LoadXml(stringwriter.ToString());
docSave.Save(@"D:\Info.xml");
Console.ReadLine();
}
}
}
<?xml version="1.0" encoding="utf-16"?>
<Persons>
<Person>
<Name>Sourav</Name>
</Person>
</Persons>First
of all we have to include one namespace called System.IO in our c# file because
in this example we are using StringWriter class and this class belongs to
System.IO namespace. Let’s discuss code snippet line by line.
StringWriter stringwriter = new StringWriter();
XmlTextWriter xmlTextWriter = new XmlTextWriter(stringwriter);
In first line we are creating
object of StringWriter
class and in second line object of XmlTextWriter class. And during object creation of XmlTextWriter
class we are sending object of StringWriter class a argument of XmlTextWriter
class constructor.
xmlTextWriter.Formatting
= Formatting.Indented;
In
this line we are setting Formatting property of xmlTextWriter object using the enumerator
of Formatting class.If the Indented option is set,
child elements are indented using the Indentation and IndentChar properties.
xmlTextWriter.WriteStartDocument();
xmlTextWriter.WriteStartElement("Persons");
xmlTextWriter.WriteStartElement("Person");
xmlTextWriter.WriteElementString("Name", "Sourav");
The
first line of this code snippet is for starting of XML Document. The next two
line will create two element followed by Persons element which is again root
element like previous example.
Then
the line xmlTextWriter.WriteElementString("Name", "Sourav");
will add one element as a child element of Person element with value Sourav.
And the newly added element name is “Name” as the first argument of
WriterElementString() function.
Below
two lines of code is for closing the document.
xmlTextWriter.WriteEndElement();
xmlTextWriter.WriteEndDocument();
Now
we have to create object of XmlDocument class to load our formatted XML String
and to save in persistent storage location using below code.
XmlDocument docSave = new
XmlDocument();
docSave.LoadXml(stringwriter.ToString());
docSave.Save(@"D:\Info.xml");
Conclution:
Those
examples are very simple and only for demonstration purpose. In real scenario may
experience big XML document with hundreds of element and attributes.