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 9002957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T00:38:42+00:00 2026-06-16T00:38:42+00:00

I have a probleme to parse multiple XML field This a the style of

  • 0

I have a probleme to parse multiple XML field

This a the style of the XML :

<students>
  <student>
    <student_id>1</student_id>
    <student_name>Mike</student_name>
    <subjects>
      <subject>
        <school_subject>History</school_subject>
      </subject>
      <subject>
        <school_subject>Maths</school_subject>
      </subject>
      <subject>
        <school_subject>English</school_subject>
      </subject>
    </subjects>
  </student>
  <student>
    ...
  </student>
</students>

I can parse it for the simple field like student_id and student_name but when there are multiple repetition of a same field I don’t know how to do :/
This is my source code of the parsing.

I made two classes :

  • Subject containing the name of the school subject
  • student containing id and name and a list of class Subject.

Now the source code :

List<Student> L1 = new List<Student>();

XDocument doc = XDocument.Load(s);
var q = from b in doc.Descendants("student")
select new
{
    s_id = (string)b.Element("student_id"),
    s_name = (string)b.Element("student_name"),
    s_subject = (Subject)b.Elements("school_subject")
};
foreach (var p in q)
{
    L1.Add(new Student() { id = p.s_id, name = p.s_name, subject = p.s_subject });
}
listBox1.ItemsSource = L1;

Thanks for reading and sorry for my very bad english.

  • 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-16T00:38:43+00:00Added an answer on June 16, 2026 at 12:38 am

    According to your xml student can have several subjects. So, you need collection of subjects as property of student:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public List<Subject> Subjects { get; set; }
    }
    
    public class Subject
    {
        public string Name { get; set; }
    }
    

    And here is parsing. To fill get student’s subjects you need to do sub query:

    XDocument xdoc = XDocument.Load(s);
    IEnumerable<Student> students =
        from s in xdoc.Descendants("student")
        select new Student()
        {
            Id = (int)s.Element("student_id"), // you can cast to int
            Name = (string)s.Element("student_name"),
            Subjects = s.Element("subjects") // here goes sub query
                        .Elements("subject")
                        .Select(subj => new Subject() { 
                           Name = (string)subj.Element("school_subject") 
                        }).ToList()
        };
    

    BTW I think you can use simple string to hold subject name – you don’t really need class for that.

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

Sidebar

Related Questions

I have been creating multiple background threads to parse xml files and recreate new
I have a problem with using the SAX parser to parse a XML file.
I have a problem with this code right here: - (void)fetchedData:(NSData *)responseData { //parse
I have multiple CSV files which I need to parse in a loop to
I have concatenation problem for multiple string tags in Sax Parser. My Xml Data
I parse an XML file with PHP. My problem is I have a dynamic
I have a single xml parsing function that I'm trying to call multiple times
i have xsd and xml file. xml parse fine when validation is turned off.
I use a database with multiple tables in my application. I have an XML
What I want to do: i) Parse XML data into a select option field

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.