Solved

LINQ for reading an XML file

Posted on 2014-10-16
7
182 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
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

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

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!

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…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

756 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