Binding results of an linq to xml query to the sam

2019-08-21 12:08发布

问题:

How can I bind the results of my linq to xml query parser to the same gridview. I'm only able to see the last one for the ZAxisCalib/query3.

 string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            XDocument doc = XDocument.Load(fileName);
            var query = from x in doc.Descendants("XAxisCalib")
                        select new
                        {

                            MaxChild = x.Descendants("Max"),
                            MinChild = x.Descendants("Min")
                        };
            {
                var bs1 = new BindingSource { DataSource = query };

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

                dataGridView1.DataSource = bs1;
            }


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

                         select new
                         {

                             MaxChild = y.Descendants("Max"),
                             MinChild = y.Descendants("Min")

                         };


            var bs2 = new BindingSource { DataSource = query2 };

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

            dataGridView1.DataSource = bs2;


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

                         select new
                         {

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

            var bs3 = new BindingSource { DataSource = query3 };

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

            dataGridView1.DataSource = bs3;

        }

回答1:

You're replacing the datasource each time. If you just want to concatenate the results of the three queries, you could use:

var source = new BindingSource { DataSouce = query1.Concat(query2)
                                                   .Concat(query3) };
dataGridView1.DataSource = source;


回答2:

try this one:

string[] fileEntries = Directory.GetFiles(@"c:\Sciclone UAC", "*.cfg*");
        foreach (string fileName in fileEntries)
        {
            XDocument doc = XDocument.Load(fileName);
            var query = from x in doc.Descendants("XAxisCalib")
                        select new
                        {

                            MaxChild = x.Descendants("Max"),
                            MinChild = x.Descendants("Min")
                        };
          /*  {
                var bs1 = new BindingSource { DataSource = query };

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

                dataGridView1.DataSource = bs1;
            }*/


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

                         select new
                         {

                             MaxChild = y.Descendants("Max"),
                             MinChild = y.Descendants("Min")

                         };


          /*  var bs2 = new BindingSource { DataSource = query2 };

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

            dataGridView1.DataSource = bs2;*/


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

                         select new
                         {

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

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

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

            dataGridView1.DataSource =bs3;

        }

I don't compile it, it is a fast sketch