linq到xml我需要做什么才能绑定到gridview?

时间:2011-09-12 19:37:05

标签: xml linq gridview dataset linq-to-xml

如何设置VSTUDIO以将我的linq绑定到xml查询结果到gridview而不是写入控制台?

namespace TestCFG

{     课程     {         公共类XAxisCalib         {

        static void Main(string[] args)
        {
            {


                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")
                                };
                    foreach (var x in query)
                    {
                        foreach (var nextLevel in x.MaxChild)
                        {
                            Console.WriteLine("" + nextLevel.Value);
                        }
                        foreach (var nextLevel in x.MinChild)
                        {
                            Console.WriteLine("" + nextLevel.Value + "\n");
                        }



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

                                     select new
                                     {

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

                                     };


                        foreach (var y in query2)
                        {
                            foreach (var nextLevel in y.MaxChild)
                            {
                                Console.WriteLine("" + nextLevel.Value);
                            }
                            foreach (var nextLevel in y.MinChild)
                            {
                                Console.WriteLine("" + nextLevel.Value + "\n");
                            }

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

                                         select new
                                         {

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

                            foreach (var z in query3)
                            {
                                foreach (var nextLevel in z.MaxChild)
                                {
                                    Console.WriteLine("" + nextLevel.Value);
                                }
                                foreach (var nextLevel in z.MinChild)
                                {
                                    Console.WriteLine("" + nextLevel.Value + "\n");
                                }

                            }
                        }
                    }

                }

            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

你可以简单地做一些事情,比如

YourDataGrid.DataSource = query3.ToList();

YourDataGrid是一个虚构的对象,应该替换为您在Windows窗体应用程序或WPF应用程序中创建的DataGrid的任何名称(尽管这更复杂)