LINQ AND XML AND GRID VIEW

LOADING DATA FROM XML TO GRID VIEW

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Security;          /* the class that contains the encrypted string*/
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing;
using System.Xml.Linq;


namespace ConsoleApplication7
{

    class GUI :Form
    {
      
       private Label nameLabel1;
       private Label nameLabel2;
       private Button nameButton;
       private ComboBox nameComBox;
       private ComboBox nameComBox1;
       private List<string> nameList;
       private List<string> nameList1;
       private DataGridView nameDataGridView;

        public GUI()
        {
            myform();
        }
  
        public void myform()
        {

           
            nameButton = new Button();
            this.nameButton.Top = 100;
            this.nameButton.Left = 100;
            this.nameButton.Text = "finished";
            this.nameButton.Click += new System.EventHandler(nameButton_Click);

            nameLabel1 = new Label();
            this.nameLabel1.Text = "Country";
            this.nameLabel1.Top = 80;
            this.nameLabel1.Left = 100;

            nameComBox = new ComboBox();
            this.nameComBox.Top = 80;
            this.nameComBox.Left = 200;
            this.nameComBox.SelectedIndexChanged += new System.EventHandler(nameComBox_SelectedIndexChanged);

            nameLabel2 = new Label();
            this.nameLabel2.Text = "Cities";
            this.nameLabel2.Top = 100;
            this.nameLabel2.Left = 250;

            nameComBox1 = new ComboBox();
            this.nameComBox1.Top = 100;
            this.nameComBox1.Left = 350;

            this.Controls.Add(nameButton);
            this.Controls.Add(nameLabel1);
            this.Controls.Add(nameComBox);
            this.Controls.Add(nameLabel2);
            this.Controls.Add(nameComBox1);



            XDocument xdoc = XDocument.Load("H:\\ConsoleApplication7\\ConsoleApplication7\\Country-City.xml");
            var countires = from country in xdoc.Descendants("countries").Elements("country").Attributes("name")
                            select country.Value;

            nameList = new List<string>();
            nameList1 = new List<string>();

            foreach (var country in countires)
            {
                nameList.Add(country.ToString());
            }

            nameComBox.DataSource = nameList;

            //ddlCountry.DataBind();

            var cities = from country in xdoc.Descendants("countries").Elements("country")
                         where country.Attribute("name").Value == nameComBox.SelectedValue.ToString()
                         from city in country.Elements("city")
                         orderby city.Value
                         select city.Value;

            foreach (var city in cities)
            {
                nameList1.Add(city.ToString());
            }

            nameComBox1.DataSource = nameList1;


        }

        void nameButton_Click(object sender, EventArgs e)
        {
            nameDataGridView = new DataGridView();
            this.nameDataGridView.Top = 250;
            this.nameDataGridView.Left= 200;
            this.Controls.Add(nameDataGridView);


            XDocument xdoc = XDocument.Parse(@"
                <Employees>
                    <Employee>
                        <Name>Thomas</Name>
                        <Designation>Executive</Designation>
                        <Department>Accounts</Department>
                        <Salary>5000</Salary>
                    </Employee>
                    <Employee>
                        <Name>Wills</Name>
                        <Designation>Manager</Designation>
                        <Department>Accounts</Department>
                        <Salary>24000</Salary>
                    </Employee>
                    <Employee>
                        <Name>Brod</Name>
                        <Designation>Manager</Designation>
                        <Department>Finance</Department>
                        <Salary>28000</Salary>
                    </Employee>
                    <Employee>
                        <Name>Smith</Name>
                        <Designation>Analyst</Designation>
                        <Department>Finance</Department>
                        <Salary>21000</Salary>
                    </Employee>
                </Employees>
                ");
            var result = from emp in xdoc.Root.Elements()
                         select new
                         {
                             EmployeeName = emp.Element("Name").Value,
                             Department = emp.Element("Department").Value,
                             Salary = emp.Element("Salary").Value
                         };
            nameDataGridView.DataSource = result.ToList();
           
           
        }

        void nameComBox_SelectedIndexChanged(object sender, EventArgs e)
        {
            List<string> list = new List<string>();

            try
            {
                XDocument xdoc = XDocument.Load("H:\\ConsoleApplication7\\ConsoleApplication7\\Country-City.xml");
                var cities = from country in xdoc.Descendants("countries").Elements("country")
                             where country.Attribute("name").Value == nameComBox.SelectedValue.ToString()
                             from city in country.Elements("city")
                             orderby city.Value
                             select city.Value;

                foreach(var city in cities)
                {
                    list.Add(city.ToString());
                }

                nameComBox1.DataSource = list;
            
               
            }
            catch
            {
            }
        }

    }
   
    class FormsDemo
    {
        public static void Main()
        {
            GUI g = new GUI();
            Application.Run(g);
        }
    }

}

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6