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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:41:15+00:00 2026-06-14T06:41:15+00:00

I’m looking to parse a relatively complex XML file through C# and store a

  • 0

I’m looking to parse a relatively complex XML file through C# and store a selection of the data into a SQL Server ’08 database. This is what I’m looking to extract from the XML file:

<educationSystem>
    <school>
        <name>Primary School</name>
        <students>
            <student id="123456789">
                <name>Steve Jobs</name>
                <other elements>More Data</other elements>
            </student>
            <student id="987654">
                <name>Jony Ive</name>
                <otherElements>More Data</otherElements>
            </student>
        </students>
    </school>
    <school>
        <name>High School</name>
        <students>
            <student id="123456">
                <name>Bill Gates</name>
                <other elements>More Data</other elements>
            </student>
            <student id="987654">
                <name>Steve Ballmer</name>
                <otherElements>More Data</otherElements>
            </student>
        </students>
    </school>
</educationSystem>

[Before you ask, no this isn’t a school assignment – I’m using school/students as an example and because the original is a lot more sensitive.]

I’m able to (using XDocument/XElement) parse the XML file and get a list of all school names, student names and student ID’s, but when this gets added to the database, I end up with the Bill Gates student entry being under a second school. It’s all just line-by-line.

I’m looking to find a way to say, achieve this:

Foreach school
    put it's name into an XElement
    foreach student
        grab the name and id put into XElements
Grab next school and repeat

I believe Linq would be the best way to achieve this, but I’m having trouble in how to get started with the process. Would anyone be able to point me in the right direction?

Edit: Here’s the code I’m currently using to save data to the database. It processes a list at a time (hence things aren’t related as they should be). I’ll also be tidying up the SQL as well.

 private void saveToDatabase (List<XElement> currentSet, String dataName)
    {
        SqlConnection connection = null;

        try
        {
            string connectionString = ConfigurationManager.ConnectionStrings["connString"].ConnectionString + "; Asynchronous Processing=true";
            connection = new SqlConnection(connectionString);
            connection.Open();

            foreach (XElement node in currentSet)
            {
                SqlCommand sqlCmd = new SqlCommand("INSERT INTO dbo.DatabaseName (" + dataName + ") VALUES ('" + node.Value + "')", connection);

                sqlCmd.ExecuteNonQuery();
            }
        }
  • 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-14T06:41:16+00:00Added an answer on June 14, 2026 at 6:41 am

    This LINQ will generate a Collection of Objects,with two properties

    1. Name of the school

    2. List of students(again a collection)

       var result = XElement.Load("data.xml")
                     .Descendants("school")
                     .Select( x => new { 
                               name = XElement.Parse(x.FirstNode.ToString()).Value,
                               students =x.Descendants("student")
                                          .Select(stud => new { 
                     id = stud.Attribute("id"),
                     name = XElement.Parse(stud.FirstNode.ToString()).Value})
                 .ToList()});
      

      Note:The LINQ assumes <name> as the first node under <school> and <student> tags

    3. Then you can use the foreach that you intended and it will work like a charm

       foreach (var school in result)
       {
          var schoolName = school.name;
          foreach (var student in school.students)
          {
                  //Access student.id and student.name here                    
          }
       }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
In my XML file chapters tag has more chapter tag.i need to display chapters
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a reasonable size flat file database of text documents mostly saved in
I'm parsing an XML file, the creators of it stuck in a bunch social
i want to parse a xhtml file and display in UITableView. what is the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have a jquery bug and I've been looking for hours now, I can't

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.