Subscribe to Subscribers
Talk to Webmaster Tony John

Online Members

Ranjith Kumar
More...


Resources » Code Snippets » XML

Create XML File in C#.NET using XmlTextWriter


Posted Date:     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.


    Next Resource: Reading the XML Elements alone.
    Previous Resource: Display XML Data in HTML page
    Return to Resources
    Post New Resource
    Category: XML


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    Using XmlTextWriter to write XML files  .  Using XmlTextWriter to Create XML files  .  Create XML File using XmlTextWriter  .  



    Follow us on Twitter: https://twitter.com/dotnetspider

    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.