SIGN UP MEMBER LOGIN:    
ARTICLE

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

Posted by Bechir Bejaoui Articles | XML in C# June 26, 2008
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.
Reader Level:
Download Files:
 

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!!!

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Nevron Gauge for SharePoint
Become a Sponsor