user1240679
user1240679

Reputation: 6979

Generating dictionary from elements of XML through LINQ

I am experimenting with to parse an XML file, and get some output in required format.

Here is my XML file format.

<?xml version="1.0" encoding="utf-8" ?>
<Sample>
  <Student name="Tom" id="0" batch="1">
    <Performance>
      <Previous id="1">Marks 1</Previous>
      <Next mid="2">Marks 2</Next>
      <Next mid="3">Marks 3</Next>
    </Performance>
  </Student>
  <Student name="Jerry" id="1" batch="1">
    <Previous mid="1">Marks 4</Previous>
    <Next mid="2">Marks 5</Next>
    <Next mid="3">Marks 6</Next>
    <Next mid="4">Marks 12</Next>
  </Student>
  <Student name="Kate" id="5" batch="2">
    <Previous mid="2">Marks 7</Previous>
    <Previous mid="3">Marks 8</Previous>
    <Next mid="4">Marks 6</Next>
  </Student>
</Sample>

The output I am trying to get is a dictionary from this XML file:

0 - Collecion of (Previous and Next Marks) 
1 - Collecion of (Previous and Next Marks)
5 - Collecion of (Previous and Next Marks)

where 0, 1, 5 are ID's of students, and correspondingly are the collections of marks of that student.

For this, I have written this query, which is not quite giving me the output:
UPDATE (Query added)

XDocument xdoc = XDocument.Load("XMLfile1.xml");
            var content = xdoc.Descendants("Student")
                        .Select(st => st.Descendants("Previous")
                        .Union(st.Descendants("Next"))
                        .Select(terms => new Marks { MarksId = terms.Attribute("mid").Value, MarksName = terms.Value })).ToDictionary<Marks, int>(key => key.StudentId) ;

Problem:
1. I am not able to select the Attribute ID of Student nodes
2. I am not able to select the key using for the dictionary using key => key.StudentID and it giving some errors.

Class file that I am using here to parse:

class Marks
    {
        public int StudentID
        {
            get;
            set;
        }
        public string MarksId
        {
            get;
            set;
        }
        public string MarksName
        {
            get;
            set;
        }

Upvotes: 1

Views: 176

Answers (1)

the_joric
the_joric

Reputation: 12226

Check this:

var dictionary = (from student in xElement.Descendants("Student")
                    let marks = student.Descendants()
                         .Where(e => new []{"Previous" ,"Next"}
                              .Contains(e.Name.ToString()))
                    select new {student, marks})
                  .ToDictionary(t => t.student.Attribute("id").Value, 
                                  t => t.marks.Select(mark => new Data {
                                                                       MarkId = mark.Attribute("mid").Value, 
                                                                       MarkName = mark.Value
                                                                        }).ToList());

Note that your XML probably has an error: instead of <Previous id="1">Marks 1</Previous> there should be probably <Previous mid="1">Marks 1</Previous>

Upvotes: 2

Related Questions