Announcement

Thursday, 21 August 2014

Create xml file in C# .net

Creating XML file in C# .net

xml file
Students.xml


Often we require to create text file or csv file, excel file, some other kind of file for reporting purpose. As a Developer, we often require to create an xml file for easily manipulating or rendering the data on the view. XML stands for EXtensible Markup Language.

It allows us to create our own tags. Unlike HTML, it focuses on what data is rather than how data looks. XML simplifies data transportation and data sharing since data is stored in plain text format and thus provides a hardware and software independent way of storing data.

It uses System.Xml.Linq; namespace.

The XML file that we create will have the following structure:




<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!--Creating XML Document using .NET-->

<Students>
<Student Id="1">
<Name>Amit</Name>
<Gender>Male</Gender>
<TotalMarks>800</TotalMarks>
</Student>
<Student Id="2">
<Name>Amber</Name>
<Gender>Female</Gender>
<TotalMarks>900</TotalMarks>
</Student>
<Student Id="3">
<Name>Sam</Name>
<Gender>Male</Gender>
<TotalMarks>750</TotalMarks>
</Student>

</Students>



Step 1: Create an xml document using XDocument class using the constructor of params parameter that allows parameter list of content objects to add to the documnet.

Step 2: Inside the xml document we create xml declaration that specifies its version, encoding and standalone status. This is optional. Leaving it does not hinder the process of creating xml file rather .net will automatically add declaration with its default version and encoding i.e., 1.0 and utf-8 respectively.

Step 3: Next we create the comment in xml file using the XComment class and passing the comment value as string.

Step 4: Now we create Root element of xml file that is Students using XElement class.

Step 5: Now we add its first child element as Student using XElement class along with its attribute as Id and its value as 101 using XAttribute class with specified name and value i.e., Id and 101.

Step 6: Next we create three child of Student element as Name, Gender, and TotalMarks along with their values specified as strings using XElement class as follows:


new XElement("Students",

new XElement("Student",
new XAttribute("Id",1),
new XElement("Name","Amit"),
new XElement("Gender","Male"),
new XElement("TotalMarks","800")),


Step 7: In the similar manner we write code for rest of the students as per the structure of the xml file above.

Step 8: Finally we save the xml file using the Save method of the XmlDocument object of the XDocument class that we create in Step 1 passing the path as string.

The final code is here:





using System.Xml.Linq;
namespace CreateXml
{
class Program
{
static void Main(string[] args)
{
XDocument XmlDocument=new XDocument(
new XDeclaration("1.0","utf-8","yes"),
new XComment("Creating XML Document using .NET"),
new XElement("Students",
new XElement("Student",
new XAttribute("Id",1),
new XElement("Name","Amit"),
new XElement("Gender","Male"),
new XElement("TotalMarks","800")),
new XElement("Student",
new XAttribute("Id", 2),
new XElement("Name","Amber"),
new XElement("Gender","Female"),
new XElement("TotalMarks","900")),
new XElement("Student",
new XAttribute("Id", 3),
new XElement("Name","Sam"),
new XElement("Gender","Male"),
new XElement("TotalMarks","750"))));
XmlDocument.Save("E:\\DummyTest\\CreateXml\\CreateXml\\Students.xml");
}
}
}

CONCLUSION

Running the project will successfully create an Students.xml file at the specified location. For ease of purpose and to save time, I have provided the same location as that of project so that I can open the xml file just by clicking on the Show All Files icon in the solution explorer. You can provide different location as per your requirement.

FEEDBACK

Feedback is essential for me to move on and to do RnD further. So please do comment.

No comments: