Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7785423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T20:15:13+00:00 2026-06-01T20:15:13+00:00

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

  • 0

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T20:15:13+00:00Added an answer on June 1, 2026 at 8:15 pm

    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>

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a question about XML parsing. I was experimenting with a sample program
I'm getting this error (see title) while trying to parse an XML file in
Experimenting with android game development. I'm using PNG format for images, and was wondering
While experimenting with pixel shaders in WPF I decided to draw some pixels onto
I'm experimenting with XSLT2, using a stylesheet based on this answer: <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>
I have been experimenting with LINQ to XML and have run across a very
I've been experimenting with ScraperWiki and yesterday, I could get a list of all
I'm experimenting with WPF animations, and I'm a bit stuck. Here's what I need
I am, for some time, experimenting with LLVM , simply because. It does, however,
I am experimenting with calling a web-service using jQuery. For some reason the result

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.