Click here to Skip to main content
14,698,403 members
Articles » Platforms, Frameworks & Libraries » LINQ » General
Posted 16 Apr 2012

Tagged as


16 bookmarked

Convert XML to Object using LINQ

Rate me:
Please Sign up or sign in to vote.
4.79/5 (13 votes)
17 Apr 2012CPOL
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

Comments and Discussions

PraiseSimple, elegant and useful. Pin
marcelo iván rojas hernández14-Jun-18 10:39
Membermarcelo iván rojas hernández14-Jun-18 10:39 
GeneralMy vote of 5 Pin
Jesus Bocanegra12-Mar-13 6:58
MemberJesus Bocanegra12-Mar-13 6:58 
Questionthe LINQ can be reduced to a single statement too Pin
S@53K^S18-Apr-12 7:09
MemberS@53K^S18-Apr-12 7:09 
QuestionI agree with John Pin
Mehdi Gholam16-Apr-12 5:01
MemberMehdi Gholam16-Apr-12 5:01 
AnswerRe: I agree with John Pin
Naufel Basheer17-Apr-12 2:29
MemberNaufel Basheer17-Apr-12 2:29 
QuestionWhy use this over XmlSerialize? PinPopular
John Brett16-Apr-12 3:06
MemberJohn Brett16-Apr-12 3:06 
AnswerRe: Why use this over XmlSerialize? Pin
Naufel Basheer17-Apr-12 2:28
MemberNaufel Basheer17-Apr-12 2: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.