---
title: "Reading XML from LINQ"  
description: "In this blog, I’m explaining how to read the xml from linq query.Example In this example, we will have two combo boxes – country and city and we will"  
author: "Sumit Kesarwani"  
published: 2013-12-19  
updated: 2014-09-18  
canonical: https://www.mindstick.com/blog/613/reading-xml-from-linq  
category: "linq"  
tags: ["linq"]  
reading_time: 2 minutes  

---

# Reading XML from LINQ

In this blog, I’m explaining how to read the xml from [linq query](https://www.mindstick.com/forum/33908/how-to-select-and-retrieve-data-using-linq-query-in-entity-framework).

Example In this example, we will have two combo boxes – [country](https://yourviews.mindstick.com/view/85543/do-you-think-that-french-will-be-islamic-country) and city and we will [populate](https://www.mindstick.com/blog/60/populate-records-in-second-listbox-according-to-the-selection-in-first-list-box) these combo boxes from [xml file](https://www.mindstick.com/articles/75/how-to-read-and-write-xml-file-through-c-sharp) using linq query and also change the [cities](https://yourviews.mindstick.com/view/252/air-pollution-choking-many-cities) in city combo box according to country combo box value.

##### Step 1:

First create a [windows](https://www.mindstick.com/articles/311752/how-to-install-and-use-the-google-wifi-software-on-a-windows-or-mac-computer) [form application](https://www.mindstick.com/forum/34200/system-data-sqlclient-sqlexception-occurred-while-using-checked-or-radio-button-in-form-application) and [add two](https://www.mindstick.com/forum/156862/write-a-c-sharp-program-to-add-two-numbers-without-using-operator) combo box one for country and other for city like this:

![Reading XML from LINQ](https://www.mindstick.com/blogs/ac18794e-7777-4386-b812-bef0f8804705/images/db249ba9-81dd-453b-bd65-8ac7b9573a17.png)

##### Step 2:

Now create a xml file and save with “cities.xml” name:

```
<?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>
```

##### Step 3:

By using XDocument object we can load XML documents, writing linq queries on XDocument will return collection. In linq query xdocument.Descendants will return a IEnumarable XElement collection.

Now write the below code in the from load event: \

```
XDocument xdoc = XDocument.Load(“Your xml file path”); //load the xml filevar countries = from country in xdoc.Descendants("countries").Elements("country").Attributes("name")                            select country.Value; //Get all country from xml cbCountry.DataSource = countries.ToList();//Populate country combo box with countriesvar cities = from country in xdoc.Descendants("countries").Elements("country")                         where country.Attribute("name").Value == cbCountry.SelectedValue.ToString()                         from city in country.Elements("city")                         orderby city.Value                         select city.Value; //Get the cities from xmlcbCity.DataSource = cities.ToList();//Populate city combo box with cities
```

##### Step 4:

Now write the below code in the selected index changed event of country combo box:

```
XDocument xdoc = XDocument.Load(“Your xml file path”);var cities = from country in xdoc.Descendants("countries").Elements("country")                             where country.Attribute("name").Value == cbCountry.SelectedValue.ToString()                             from city in country.Elements("city")                             orderby city.Value                             select city.Value;//Get cities according to the country currently selected in country combo boxcbCity.DataSource = cities.ToList();
```

This linq query will populate the city combo box with cities according to

\

the country selected in the country combo box.

##### Output

![Reading XML from LINQ](https://www.mindstick.com/blogs/ac18794e-7777-4386-b812-bef0f8804705/images/18433ca8-6331-4006-8c51-2e9fa6d11da3.png)

---

Original Source: https://www.mindstick.com/blog/613/reading-xml-from-linq

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
