How to Generate an XML Document Programmatically-Using System.Xml.XmlWriter: Part III

In previous articles, I demonstrate how to generate a given xml file using both the DOM “Document Object Model” and the SAX “Serial Access parser for Xml” technologies. In fact, they are very interesting API for handling xml entities or files but the dot net provides us another way to achieve the same goal.

In previous articles, I demonstrated how to generate a given XML file using both the DOM "Document Object Model" technology and with SAX "Serial Access parser for Xml". In fact, they are very interesting API for handling XML entities or files but the .Net provides us another way to achieve the same goal. It is recommended under the .Net Framework 2.0. The XmlWriter is the suitable class to generate an XML file. To do this, follow this walkthrough.

Walkthrough:

static void Main(string[] args)

{

 

// Create an XmlWriterSettings instance.

XmlWriterSettings oSettings = new XmlWriterSettings();

oSettings.Indent = true;

oSettings.OmitXmlDeclaration = false;

oSettings.Encoding = Encoding.ASCII;

 

// Create the XmlWriter object and write some content.

using (XmlWriter writer = XmlWriter.Create(@"C:\FamilyFile.xml", oSettings))

{

//This define if it is a stand alone xml file or not

writer.WriteStartDocument(true);

//This is a comment

writer.WriteComment("This xml file representation is don with using System.Xml.XmlWriter");

//Start Familie element witch is the parent element

writer.WriteStartElement("Familie");

 

//Father

//Start by by create the element

writer.WriteStartElement("Father");

//Add the name attribute

writer.WriteStartAttribute("Name");

//Add the name value

writer.WriteValue("My father");

//Mark the name attribute end

writer.WriteEndAttribute();

//Add the age attribute

writer.WriteStartAttribute("Age");

//Add the age value

writer.WriteValue(60);

//Mark the age attribute end

writer.WriteEndAttribute();

//Mark the end of the element

writer.WriteEndElement();

 

//Mother

//Start by by create the element

writer.WriteStartElement("Mother");

//Add the name attribute

writer.WriteStartAttribute("Name");

//Add the name value

writer.WriteValue("My mother");

//Mark the name attribute end

writer.WriteEndAttribute();

//Add the age attribute

writer.WriteStartAttribute("Age");

//Add the age value

writer.WriteValue(55);

//Mark the age attribute end

writer.WriteEndAttribute();

//Mark the end of the element

writer.WriteEndElement();

 

//Sister

//Start by by create the element

writer.WriteStartElement("Sister");

//Add the name attribute

writer.WriteStartAttribute("Name");

//Add the name value

writer.WriteValue("My sister");

//Mark the name attribute end

writer.WriteEndAttribute();

//Add the age attribute

writer.WriteStartAttribute("Age");

//Add the age value

writer.WriteValue(20);

//Mark the age attribute end

writer.WriteEndAttribute();

//Mark the end of the element

writer.WriteEndElement();

 

//Brother

//Start by by create the element

writer.WriteStartElement("Brother");

//Add the name attribute

writer.WriteStartAttribute("Name");

//Add the name value

writer.WriteValue("My borther");

//Mark the name attribute end

writer.WriteEndAttribute();

//Add the age attribute

writer.WriteStartAttribute("Age");

//Add the age value

writer.WriteValue(17);

//Mark the age attribute end

writer.WriteEndAttribute();

//Mark the end of the element

writer.WriteEndElement();

 

//Mark the end of the main element witch is the familie element

writer.WriteEndElement();

//Mark the end of the document

writer.WriteEndDocument();

//Purge the writer

writer.Flush();

}

Console.WriteLine("Xml file created successfully!!!");

Console.Read();

}

Execute the code then browse to C:\FamilyFile.xml, open the file and observe.

Good dotneting!!!