Resources » Code Snippets » XML

Create XML File in C#.NET using XmlTextWriter


Posted Date: 15-Jul-2008  Last Updated:   Category: XML    
Author: Member Level: Gold    Points: 10



The code sample is a function CreateXmlFile that creates a Xml file with a name as specified in it's argument.

The code operates based on a XmlTextWriter object that has methods to create an XML file, specify it's encoding, start and end elements in the xml files, etc.
Some functions are used in the example.


void CreateXmlFile(String xmlFilePath)
{
XmlTextWriter xmlWriter = new XmlTextWriter(xmlFilePath, Encoding.UTF8);
xmlWriter.WriteStartDocument(true);
xmlWriter.WriteStartElement("Departments"); //Root Element
xmlWriter.WriteStartElement("Department"); //Department Element

xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Development"); //Attribute Value
xmlWriter.WriteEndAttribute();

xmlWriter.WriteStartElement("Employees"); //Started Employees Element
xmlWriter.WriteStartElement("Employee"); //Started Employee Element

xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Sabu C.Alex"); //Attribute Value
xmlWriter.WriteEndAttribute();

xmlWriter.WriteStartAttribute("Age");//Attribute "Age"
xmlWriter.WriteString("28");//Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteString("Sabu C.Alex is working as a Software Engineer"); //Employee Element Inner Text
xmlWriter.WriteEndElement(); //End of Employee Element

xmlWriter.WriteStartElement("Employee"); //Started Employee Element
xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Dinup Varghese"); //Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteStartAttribute("Age");//Attribute "Age"
xmlWriter.WriteString("28");//Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteString("Dinup Varghese is working as a Software Engineer"); //Employee Element Inner Text
xmlWriter.WriteEndElement(); //End of Employee Element
xmlWriter.WriteEndElement(); //End of Employees Element
xmlWriter.WriteEndElement(); //End of Department Element


xmlWriter.WriteStartElement("Department"); //Department Element

xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Accounts"); //Attribute Value
xmlWriter.WriteEndAttribute();

xmlWriter.WriteStartElement("Employees"); //Started Employees Element
xmlWriter.WriteStartElement("Employee"); //Started Employee Element

xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Mathew"); //Attribute Value
xmlWriter.WriteEndAttribute();

xmlWriter.WriteStartAttribute("Age");//Attribute "Age"
xmlWriter.WriteString("28");//Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteString("Mathew is working as an Account"); //Employee Element Inner Text
xmlWriter.WriteEndElement(); //End of Employee Element

xmlWriter.WriteStartElement("Employee"); //Started Employee Element
xmlWriter.WriteStartAttribute("Name"); //Attribute "Name"
xmlWriter.WriteString("Edwin"); //Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteStartAttribute("Age");//Attribute "Age"
xmlWriter.WriteString("28");//Attribute Value
xmlWriter.WriteEndAttribute();
xmlWriter.WriteString("Edwin is working as a Junior Accountant"); //Employee Element Inner Text
xmlWriter.WriteEndElement(); //End of Employee Element
xmlWriter.WriteEndElement(); //End of Employees Element
xmlWriter.WriteEndElement(); //End of Department Element
xmlWriter.WriteEndElement(); //End of Root Element
xmlWriter.WriteEndDocument();
xmlWriter.Flush();
xmlWriter.Close();
}


Did you like this resource? Share it with your friends and show your love!

Responses to "Create XML File in C#.NET using XmlTextWriter"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India