Solved

LINQ for reading an XML file

Posted on 2014-10-16
7
189 Views
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.
Sample.xml
0
Comment
Question by:BYTECHINDIA
[X]
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
7 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 40386304
which issus?
0
 

Author Comment

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

Accepted Solution

by:
Fernando Soto earned 500 total points
ID: 40386691
Hi BYTECHINDIA;

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

0
Industry Leaders: 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

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

Author Comment

by:BYTECHINDIA
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..
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 40387991
Hi BYTECHINDIA;

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.

Thanks.
0
 

Author Comment

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

Featured Post

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Read about why website design really matters in today's demanding market.
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

707 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