C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Interview   Jobs   Projects   Offshore Development    
Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing | Talk to Us |



My Profile

Gifts

Active Members
TodayLast 7 Days more...









Create XML File in C#.NET using XmlTextWriter


Posted Date: 15 Jul 2008    Resource Type: Code Snippets    Category: XML

Posted By: Sabu C Alex       Member Level: Gold
Rating:     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();
}




Responses


No responses found. Be the first to respond and make money from revenue sharing program.

Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Using XmlTextWriter to write XML files  .  Using XmlTextWriter to Create XML files  .  Create XML File using XmlTextWriter  .  Create XML File Using C#.NET  .  Create XML File in C#.NET using XmlTextWriter  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Reading the XML Elements alone.
Previous Resource: Display XML Data in HTML page
Return to Discussion Resource Index
Post New Resource
Category: XML


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

doors in nj

Contact Us    Privacy Policy    Terms Of Use