Solved

I want to do searching on the basis of first and third character i.e. a*d using LINQ from an xml file.How should i do it?

Posted on 2014-10-20
4
118 Views
Last Modified: 2016-02-16
Dear All

I have an XML file in which I am searching city's on the basis of filter containing first three characters,which is working fine.
 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(substr)
                                    select city;
Now I want to do searching on the basis of only first and third character,second character can be anything.
How should I do it?
0
Comment
Question by:BYTECHINDIA
  • 3
4 Comments
 
LVL 62

Accepted Solution

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

Modifying the query like this should give you what you are looking for.

// Searching for first and third character in the CITY name
// Test data
string first = "A";
string third = "l";
 
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 - Modified line in query
            where city.Value.StartsWith(first) && city.Value.SubString(2, 1) == third 
            select city;

Open in new window

0
 

Author Comment

by:BYTECHINDIA
ID: 40393614
ohh it was too simple.Actually I have started looking using SQLMethod function and get lost somewhere else.
Thank you sir,I will try this.
0
 

Author Comment

by:BYTECHINDIA
ID: 40393616
Its working perfectly.Can I check two attributes too using same LINQ query i.e. state is Delhi and city starts with filter?If yes,please explain.
0
 

Author Comment

by:BYTECHINDIA
ID: 40394398
Thank you sir.
I did it as below and succeeded.

 IEnumerable<XElement> Region = from data in doc.Root.Elements(rs + "data")
                                               from el in data.Elements(z + "row")
                                               where el.Attribute("REGION_NAME").Value == State
                                               select el;

                Locations = from city in Region.Attributes("CITY")
                            where city.Value.StartsWith(Filter)
                            select city;
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

Suggested Solutions

Title # Comments Views Activity
Does this SQL code look like it will work using VS2010 with .NetFramework 4.0 ? 2 42
Allow space in this pattern 2 48
Syntax error 9 45
Wav problem 4 16
This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
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…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

896 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

13 Experts available now in Live!

Get 1:1 Help Now