LINQ for reading an XML file

Posted on 2014-10-16
Last Modified: 2016-02-15
Dear All

Using LINQ,I wanted to search all the cities starting with specific series of characters from the attached sample XML  file format.
I am completly new to LINQ.
I am using following lines for the extraction,.but facing some issue.Please help.

var Locations = from city in doc.Root.Elements("row").Attributes("CITY")
                                where city.Value.StartsWith(Filter)
                                select city;

Thanks in advance.
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
LVL 70

Expert Comment

by:Éric Moreau
ID: 40386304
which issus?

Author Comment

ID: 40386319
Sir,instead of array of city in Locations variable I am getting System.Linq.Enumerable+WhereEnumerableIterator`1[System.Xml.Linq.XAttribute].
LVL 63

Accepted Solution

Fernando Soto earned 500 total points
ID: 40386691

The XML document you have is using XML name spaces and they need to be used in the query to get access to the nodes you need as shown below. Also note that Linq queries use the deferred execution model, which means it will not execute until it is enumerated over. So if you need it to execute immediately wrap the query in ( ) and apply the ToList method on it. For example ( query here ).ToList();.

// Name spaces used within the document must be used to access the data
XNamespace z = "#RowsetSchema";
XNamespace rs = "urn:schemas-microsoft-com:rowset";
// Get access to the parent node using the XNamespace rs
var Locations = from data in doc.Root.Elements(rs + "data")
                // Get access to the child nodes using the XNamespace z
                from city in data.Elements(z + "row").Attributes("CITY")
                // Filter the result set
                where city.Value.StartsWith(Filter)
                select city;

Open in new window

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 40387987
Thank you so much sir.. It worked.. Thanks a lot...

Author Comment

ID: 40388615
I've requested that this question be closed as follows:

Accepted answer: 0 points for BYTECHINDIA's comment #a40387987

for the following reason:

I applied the code and it worked.. Thats great..
LVL 63

Expert Comment

by:Fernando Soto
ID: 40387991

Did you mean that you applied the code I posted and it worked? If that is the case you selected the wrong solution as the accepted solution and please have the question re-open so that you may select the correct answer.


Author Comment

ID: 40388613
I want to reopen this question just because I think by mistake I have accepted the wrong answer as solution.

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
c# DateTime Format validation 4 87
Create a Dropdown list with sub group 10 38
Trigger C# code inside the SQL Server 6 56
Example code 13 36
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
In this article we will discuss all things related to StageFright bug, the most vulnerable bug of android devices.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question