Solved

LINQ for reading an XML file

Posted on 2014-10-16
7
177 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
  • 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 62

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
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 

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 62

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now