?
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
Medium Priority
?
126 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
[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
  • 3
4 Comments
 
LVL 63

Accepted Solution

by:
Fernando Soto earned 2000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses
Course of the Month15 days, 15 hours left to enroll

743 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