Create XML File in C#.NET using XmlTextWriter

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();
}


Comments

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