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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T13:33:24+00:00 2026-05-25T13:33:24+00:00

How come during the linq to xml parse my code is only reading one

  • 0

How come during the linq to xml parse my code is only reading one fo the xml files in the folder rather than all of them?

private void button1_Click(object sender, EventArgs e)
{
    string[] fileEntries = Directory.GetFiles(@"", "*.cfg*");
    foreach (string fileName in fileEntries)
    {
        XDocument doc = XDocument.Load(fileName);
        var query = from x in doc.Descendants("")
                    select new
                    {

                        MaxChild = x.Element("Max").Value,
                        MinChild = x.Element("Min").Value
                    };


        var query2 = from y in doc.Descendants("")

                     select new
                     {

                         MaxChild = y.Element("Max").Value,
                         MinChild = y.Element("Min").Value

                     };


        var query3 = from z in doc.Descendants("")

                     select new
                     {

                         MaxChild = z.Element("Max").Value,
                         MinChild = z.Element("Min").Value
                     };

        var bs3 = new BindingSource { DataSource = query.Union(query2.Union(query3)) };

        dataGridView1.AutoGenerateColumns = true;
        dataGridView1.AutoSize = true;

        dataGridView1.DataSource = bs3;
        dataGridView1.Columns[0].HeaderText = "Max";
        dataGridView1.Columns[1].HeaderText = "Min";

    }  
}
  • 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-05-25T13:33:25+00:00Added an answer on May 25, 2026 at 1:33 pm

    You’re iterating over the files in the directory – but simply reassigning the datasource each time. In other words, it’s exactly the same problem as in your earlier question, except that instead of it being to do with three queries, it’s to do with reassigning it for each file.

    Try this instead, using a single query:

    var query = from file in fileEntries
                let doc = XDocument.Load(file)
                from x in doc.Descendants("XAxisCalib")
                             .Concat(doc.Descendants("YAxisCalib"))
                             .Concat(doc.Descendants("ZAxisCalib"))
                select new
                {
                    // Include this to see the file for each value
                    File = file,
                    MaxChild = x.Element("Max").Value,
                    MinChild = x.Element("Min").Value
                };
    
    var bs3 = new BindingSource { DataSource = query };
    
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.AutoSize = true;
    
    dataGridView1.DataSource = bs3;
    

    EDIT: Okay, if you know there’s going to be exactly one of each element, it’s fairly easy:

    var query = from file in fileEntries
                let doc = XDocument.Load(file)
                let x = doc.Descendants("XAxisCalib").Single()
                let y = doc.Descendants("YAxisCalib").Single()
                let z = doc.Descendants("ZAxisCalib").Single()
                select new
                {
                    // Include this to see the file for each value
                    File = file,
                    MaxChildX = x.Element("Max").Value,
                    MinChildX = x.Element("Min").Value,
                    MaxChildY = y.Element("Max").Value,
                    MinChildY = y.Element("Min").Value,
                    MaxChildZ = z.Element("Max").Value,
                    MinChildZ = z.Element("Min").Value
                };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

During the parsing of certain xml files, I come across a situation that I
Is it possible to add come comment at the beginning of files during checkout
During a code review I've come across some code that defines a simple structure
During debugging in Eclipse , my code went into the while loop ( I
One issue that comes up during Pinax development is dealing with development versions of
During my day-to-day work, I tend to come across data that I want to
This may well have come up before but the following code is taken from
I know about test accounts, but during beta I'd like to allow access only
I've just come across some code in Three20 that looks like this: SEL sel
I have come across a somewhat annoying problem during a project. I created this

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.