Introduction
In this tip, I’m explaining how we can convert XML into an object using XML Serialization.
Background
The same using LINQ is available in: http://www.codeproject.com/Tips/366993/Convert-XML-to-object-using-LINQ.
Using the code
For this example, I am using the following XML:
<ArrayOfStudent>
<Student>
<Name>Name1</Name>
<Batch>I</Batch>
<School>11/7/1997</School>
<marks>
<mark>
<term>I</term>
<science>46</science>
<mathematics>50</mathematics >
<language>46</language>
<result>Pass</result>
<comments>
<teacher>good</teacher>
<parent></parent>
</comments>
</mark>
</marks>
</Student>
</ArrayOfStudent>
Here the XML format is fairly simple – need to convert this xml to a class object containing object array (for <marks
> node) using XML Serialization
A brief explanation about this XML structure
From top node–
How do we convert this to an object?
It is really simple ... We start from bottom i.e., <comments>.
- Each student has name, batch and school
- Each student has multiple term marks (e.g.: First Term/Second Term etc..)
- In each term there will be separate comment about student performance (teacher/parent)
Step I: Convert <comments>
to a class:
<comments>
<teacher>gooSDFSd</teacher>
<parent></parent>
</comments>
public class Comment
{
[XmlElement("teacher")]
public string TeacherComment { get; set; }
[XmlElement("parent")]
public string ParentComment { get; set; }
}
Tip: The XmlElement
Attribute maps the property name with the XML node, i.e., the
TeacherComment
property maps with the XML node teacher
.
Step II: Convert <mark>
to class.
<marks>
<mark>
<term>I</term>
<science>46</science>
<mathematics>50</mathematics >
<language>46</language>
<result>Pass</result>
<comments>
<teacher>good</teacher>
<parent></parent>
</comments>
</mark>
</marks>
[XmlType("mark")]
public class Mark
{
[XmlElement("term")]
public string Term { get; set; }
[XmlElement("science")]
public string Science { get; set; }
[XmlElement("mathematics")]
public string Mathematics { get; set; }
[XmlElement("language")]
public string Language { get; set; }
[XmlElement("result")]
public string Result { get; set; }
[XmlElement("comments")]
public Comment objComment = new Comment();
}
Here we bind Comment with in Mark class using as code below
[XmlElement("comments")]
public Comment objComment = new Comment();
As I mentioned in top a student can have multiple marks based on the term/session.
So we need to create List<T>
of marks as below:
[XmlArray("marks")]
public List<Mark> objMarkList = new List<Mark>();
Complete code looks like...
public class Student
{
[XmlElement("Name")]
public string StudentName { get; set; }
[XmlElement("Batch")]
public string Batch { get; set; }
[XmlElement("School")]
public string School { get; set; }
[XmlArray("marks")]
public List<Mark> objMarkList = new List<Mark>();
}
Now we have created Student class with Mark which contain Comment
Let’s move to Deserialization.
XmlSerializer deserializer = new XmlSerializer(typeof(List<Student>));
TextReader textReader = new StreamReader(@"XML file path");
List<Student> stud;
stud = (List<Student>)deserializer.Deserialize(textReader);
textReader.Close();
foreach (var _stud in listStd)
{
}
What is happening here!!!
XmlSerializer deserializer = new XmlSerializer(typeof(List<Student>));
XmlSerializer
deserializes from a stream , so we create a file stream from our XML file ,
TextReader textReader = new StreamReader(@"XML file path");
Then simply call Deserialize
on the stream and cast the output to our desired type. Now the students
list is populated with objects.