Click here to Skip to main content
12,549,996 members (51,867 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


14 bookmarked

Convert XML to Object using LINQ

, 17 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Convert complex XML to an object using LINQ


I had a requirement as part of my assignment to convert complex XML to an object using LINQ.

The same thing using XML serialization and deserialization is available at this link.

Using the Code

We need to create the XML file in this format:

  <school>School Name</school >
    <mathematics>50</mathematics >
    <mathematics >45</mathematics>
     <teacher>need to improve</teacher>

Here the XML format is fairly simple – we need to convert this XML to a class object containing object array (for <marks> node) using LINQ.

A brief explanation about this XML structure is given below.

From Top Node

  • Each student has name, batch and school
  • Each student has multiple term marks (example, First Term/Second Term, etc.)
  • In each term, there will be separate comments about student performance (teacher/parent)

How do we convert this as an object?

It is really simple ... we start from the bottom i.e., <comments>.

Step I: Convert <comments> as class:

  <teacher>need to improve</teacher>
public class Comment
    private string _teacherComment;
    private string _parentComment;
    public string TeacherComment
        get { return _teacherComment; }
        set { _teacherComment = value; }
    public string ParentComment
        get { return _parentComment; }
        set { _parentComment = value; }

Step II: Convert <mark> as class:

    <mathematics>50</mathematics >
public class Mark
  private string _term;
  private string _science;
  private string _mathematics;
  private string _language;
  private string _result;
  public string Term
   get { return _term; }
   set { _term = value; }
 public string Science { get / set }
 public string Result { get / set }
 public Comment objComment = new Comment();

Here we added Comment within Mark class -i.e.,

public Comment objComment = new Comment();

As I mentioned at the top, a student can have multiple marks based on the term/session. so we need to create List <T> of marks as below:

List<Mark> objMarkList = new List<Mark>();

Step III: Create a class for <student>:

public class Student
    private string _studentName;
    private string _batch;
    private string _school;
    public string StudentName 
        { get { return _studentName; } set { _studentName = value; } }
    public string Batch 
        { get { return _batch; } set { _batch = value; } }
    public string School 
        { get { return _school; } set { _school = value; } }
    public List<Mark> objMarkList = new List<Mark>();

We have created a Student class with Mark which contains Comment.

Let’s move to LINQ

XDocument xdoc1 = XDocument.Load(“your xml Path”);
Student objStudent = new Student(); 
List<Student> lstStudent
   = (from _student in xdoc1.Element("students").Elements("student")
      select new Student
          StudentName = _student.Element("name").Value,
          Batch = _student.Element("batch").Value,
          School = _student.Element("school").Value,
          objMarkList = (from _marks in _student.Element("marks").Elements("mark")
                         select new Mark
                             Term  = _marks.Element("term").Value,
                             Science  = _marks.Element("science").Value,
                             Mathematics  = _marks.Element("mathematics").Value,
                             Language  = _marks.Element("language").Value,
                             Result = _marks.Element("result").Value,
                             objComment = (from _cmt in _marks.Elements("comments")
                                           select new Comment
                                               TeacherComment = _cmt.Element("teacher").Value,
                                               ParentComment = _cmt.Element("parent").Value
foreach (var _stud in lstStudent)
  // Your code


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Naufel Basheer
Technical Lead
India India
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralMy vote of 5 Pin
Jesus Bocanegra12-Mar-13 5:58
memberJesus Bocanegra12-Mar-13 5:58 
Questionthe LINQ can be reduced to a single statement too Pin
Sastry_kunapuli18-Apr-12 6:09
memberSastry_kunapuli18-Apr-12 6:09 
QuestionI agree with John Pin
Mehdi Gholam16-Apr-12 4:01
mvpMehdi Gholam16-Apr-12 4:01 
AnswerRe: I agree with John Pin
Naufel Basheer17-Apr-12 1:29
memberNaufel Basheer17-Apr-12 1:29 
QuestionWhy use this over XmlSerialize? PinPopular
John Brett16-Apr-12 2:06
memberJohn Brett16-Apr-12 2:06 
It would be useful if you could explain why you don't just use the built-in Xml serialization support.
There are a number of reasons why you might choose to do this, but there are also reasons for not doing so.
How, for example, do you manage and justify the fragility of having the serialization code duplicate the object layout and hierarchy?
AnswerRe: Why use this over XmlSerialize? Pin
Naufel Basheer17-Apr-12 1:28
memberNaufel Basheer17-Apr-12 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.161021.1 | Last Updated 18 Apr 2012
Article Copyright 2012 by Naufel Basheer
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid