need to write linq query for dropdownlist2 based on the selection of the values selected on the dropdownlist1 in asp.net

Actually in the case of web application, there will be 2 dropdownlists will be avaliable. Example: Country,City. Based on the selection of country the cities drop down list need to be populate. But actually the data from the dropdownlist are avaliable at xml file.And here populating the values from xml file need to write in LINQ Queries.

 protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var cities = (from x in xDocument.Descendants("countries").Descendants("country")
                        where x.Value==DropDownList1.SelectedValue
                        select new { x=x.Elements("city") }).ToList();
            DropDownList2.DataSource = cities;
            DropDownList2.DataTextField = "x";
            DropDownList2.DataValueField = "x";
            DropDownList2.DataBind();
         
        }
i had written another way as well but here also iam getting count as 0
(from x in xDocument.Descendants("countries").Descendants("country").Elements("city")                          
where x.Parent.Value.ToString()==DropDownList1.SelectedValue                          select new { x=x.Value }).ToList();
i had written code as seen above. but iam getting count as 0.as the data in xml file was present. please suggest
Gayathri GrandheAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Gayathri GrandheAuthor Commented:
var cities = (from x in xDocument.Descendants("countries").Elements("country")
                             where(x.Attribute("name")?.Value == DropDownList1.SelectedValue)
                             from y in x.Elements("city")
                                                        select new { x=y.Value }).ToList();

that is the linq query we need to follow.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.