Wednesday, October 12, 2016

How to write an XML file using XmlWriter

XmlWriter :


XmlWriter class writes XML data to a stream or a file in a fast and non-cached way.
Some important methods of XmlWriter are the following :

Create(String) :
This method creates an XmlWriter object using the specified filename.

Create(Stream) :
This method creates an XmlWriter object using the specified stream.

Close() :
This method is used to close the current stream and its underlying stream.

Dispose() :
This method disposes all resources of the XmlWriter object.

Flush() :
This method flushes whatever in the buffer of the current stream to the underlying
stream and also it flushes from the underlying stream.

WriteAttributeString(String,String) :
This method is used to write the attribute with the specified name and value.

WriteComment(String) :
This method writes comment with the specified string inside <!--...-->

WriteElementString(String,String) :
This method writes an element with the specified name and value.

WriteEndAttribute() :
This method closes the previous WriteStartAttribute call.

WriteEndDocument() :
This method closes any open elements or attributes and puts the writer back to
the start state.

WriteEndElement() :
This method closes the current element.

WriteNode(XmlReader,Boolean) :
This method copies everything from the XmlReader to the XmlWriter and moves the
reader to the start of the next sibling.

WriteNode(XPathNavigator,Boolean) :
This method copies everything from the XPathNavigator to the XmlWriter and
XPathNavigator will remain in the same position.

WriteStartAttribute(String) :
This method writes the start of an attribute with the specified name.

WriteStartDocument() :
This method writes the XML declaration with the version "1.0".

WriteStartElement(String) :
This method writes the start tag of the element with the specified local name.

WriteString(String) :
This method writes the specified text content.

WriteValue(String) :
This writes value which is a String.
Like this there are other parameter datatypes for WriteValue like DateTime, Int32,
Int64, Boolean, Decimal, Double, etc.

Next I will show one example for the code to write XML file using XmlWriter on a
Button click.

C# Code :


protected void Button1_Click(object sender, EventArgs e)
{
        XmlWriter xmlWriter = XmlWriter.Create(Server.MapPath("~/employee.xml"));

        xmlWriter.WriteStartDocument();
        xmlWriter.WriteStartElement("employees");

        xmlWriter.WriteStartElement("employee");
        xmlWriter.WriteAttributeString("post", "Manager");
        xmlWriter.WriteString("Anoop");
        xmlWriter.WriteEndElement();

        xmlWriter.WriteStartElement("employee");
        xmlWriter.WriteAttributeString("post", "Programmer");
        xmlWriter.WriteString("Lia");

        xmlWriter.WriteEndDocument();
        xmlWriter.Close();
 }

Output :

This will generate an XML file named employee.xml in the root directory of the
website with the following contents :

<?xml version="1.0" encoding="utf-8"?>
<employees>
<employee post="Manager">
Anoop
</employee>
<employee post="Programmer">
Lia
</employee>
</employees>



No comments:

Post a Comment