Announcement

Friday, 17 October 2014

Create xml file using XmlSerializer

System.Xml.Serialization_techiners
In my previous post Create xml file in C# .net we have seen the method of creating xml file using C# with XDocument class which is in System.Xml.Linq namespace. There are many ways of creating XML file in C#. One of the method is using XmlSerializer which is in System.Xml.Serialization namespace. Serialization is the process of converting object types to basic types and Deserialization is vice-versa. XmlSerialization allows us to create xml file from
objects. That is, it converts objects into Xml and it is always easy to manipulate objects. So, a good approach is to use XmlSerializer for creating xml file.

As my blog description says: The more you share, the more you possess. I shared my post on LinkedIn and I received valuable comments from Software Engineers around the world. I therefore give the credit for this post to the following Software Engineers/Developers:
who have spent their valuable time in reading and analyzing my post and honestly provided their valuable feedback. A big thanks to all of you.

NOTE:- Readers who are reading this post first time are requested to first read Create xml file in C# .net. The xml file that we will create using Serialization will have the same structure as it was in Create xml file in C# .net.



Now, let us begin step by step procedure of creating XML file using XmlSerializer.

Step 1: First we need to create a class Student that will contain following public properties:

[XmlAttribute]
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int TotalMarks { get; set; }


Since Id is an attribute of Student, we will decorate it with [XmlAttribute].

Step 2: Now, we will create another cs file within which we will define a static method of void return type. This will accept List of Students as a parameter. It is in this method that we will code for serializing the List of Students object using XmlSerializer. XmlSerializer is a class that serializes objects of specified type into XML documents and deserialize XML documents into objects of specified type.

static public void Serialize(List<student> studentsList)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<student>));
using (TextWriter writer = new StreamWriter(@"E:\DummyTest\CreateXmlUsingXmlSerializer\CreateXmlUsingXmlSerializer\Student.xml"))
{
serializer.Serialize(writer, studentsList);
}
}

TextWriter is an abstract class that can write a sequential series of characters. Using the Serialize method of the XmlSerializer class we will now serialize the list of students i.e., list of students will be converted into the required XML file.

Step 3: Now in our main method we will create list of students in which we will add required student by creating required number of student objects of type class Student that we created in Step 1 and assigning values to the properties of Student i.e., Id, Name, Gender, and TotalMarks.


List<Student> students = new List<Student>();

Student studentDetails1 = new Student();
Student studentDetails2 = new Student();
Student studentDetails3 = new Student();

studentDetails1.Id = 1;
studentDetails1.Name = "Amit";
studentDetails1.Gender = "Male";
studentDetails1.TotalMarks = 800;

students.Add(studentDetails1);

studentDetails2.Id=2;
studentDetails2.Name="Amber";
studentDetails2.Gender="Female";
studentDetails2.TotalMarks=900;

students.Add(studentDetails2);

studentDetails3.Id=3;
studentDetails3.Name="Sam";
studentDetails3.Gender="Male";
studentDetails3.TotalMarks=750;

students.Add(studentDetails3);


Step 4: After adding all student objects in the list of students we will now call our Serialize method that we created in Step 2 and pass this list as a parameter to it.

Serialize(students);

Step 5: That's it. We are done. Save the file and execute it. The XML file will get created as per our structure.

Here is the complete code for Student class.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace CreateXmlUsingXmlSerializer
{

public class Student
{
[XmlAttribute]
public int Id { get; set; }
public string Name { get; set; }
public string Gender { get; set; }
public int TotalMarks { get; set; }
}
}


Code for serializing the student object:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace CreateXmlUsingXmlSerializer
{
class CreateXmlUsingXmlSerialize
{
static void Main(string[] args)
{

List<student> students = new List<student>();

Student studentDetails1 = new Student();
studentDetails1.Id = 1;
studentDetails1.Name = "Amit";
studentDetails1.Gender = "Male";
studentDetails1.TotalMarks = 800;

students.Add(studentDetails1);

Student studentDetails2 = new Student();
studentDetails2.Id=2;
studentDetails2.Name="Amber";
studentDetails2.Gender="Female";
studentDetails2.TotalMarks=900;

students.Add(studentDetails2);

Student studentDetails3 = new Student();
studentDetails3.Id=3;
studentDetails3.Name="Sam";
studentDetails3.Gender="Male";
studentDetails3.TotalMarks=750;

students.Add(studentDetails3);
Serialize(students);
}

static public void Serialize(List<student> studentsList)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<student>));
using (TextWriter writer = new StreamWriter(@"E:\DummyTest\CreateXmlUsingXmlSerializer\CreateXmlUsingXmlSerializer\Student.xml"))
{
serializer.Serialize(writer, studentsList);
}
}
}
}

CONCLUSION

So, we have seen how to create XML file using XmlSerializer. One thing you might have notice that the XML file has one extra tag that was not there in the xml file created using XDocument. This is because we are passing a list of students to the serializer method. The advantage of it is that we won't loose object oriented approach. Besides, it is easy to convert object/s into XML and XML into Object/s. Once again thanks to all honest readers and developers around the globe for providing their valuable comments and encouraging me to write this post.

FEEDBACK

Hope the post will be useful for you in your programming career. What I need from you is to comment below and share with me your doubts and/or suggestions. Thank you :) Have a wonderful programming.

No comments: