LINQ and XML POC

THE CODE IS GIVEN BELOW- 


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;


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

           
            nameButton = new Button();
            this.nameButton.Top = 100;
            this.nameButton.Left = 100;
            this.nameButton.Text = "finished";

            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 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);
        }
    }

}


AND THE XML FILE IS PLACED IN THE BELOW LOCATION

H:\\ConsoleApplication7\\ConsoleApplication7\\Country-City.xml

<?xml version="1.0" encoding="UTF-8"?>

-<countries>
-<country name="India">
 <city>Delhi</city>
 <city>Mumbai</city>
 <city>Hyderabad</city>
 </country>
-<country name="Australia">
 <city>Sydney</city>
 <city>Hobart</city>
 <city>Canberra</city>
</country>
-<country name="Canada">
 <city>Toronto</city>
 <city>Niagara</city>
 <city>Victoria</city>
</country>
-<country name="USA">
 <city>Dallas</city>
 <city>Washington</city>
 <city>Chicago</city>
</country>
</countries>

Comments

Popular posts from this blog

Authentication and Authorization in Web API -Part1

My Gardening Journey 6