Solved

LINQ for reading an XML file

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

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 how to create custom column layout styles for Bootstrap. The article uses 5 columns to illustrate the concept, but the principle can be extended to any number of columns.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
A company’s greatest vulnerability is their email. CEO fraud, ransomware and spear phishing attacks are the no1 threat to a company’s security. Cybercrime is responsible for the largest loss of money to companies today with losses projected to r…

930 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

11 Experts available now in Live!

Get 1:1 Help Now