Solved

LINQ to XML Use Of Contains For String Compare In Where

Posted on 2009-04-06
2
841 Views
Last Modified: 2013-11-11
Hello everyone.
I am trying to check for the existence of a string within the value of an XML element while in a WHERE statement but am having no success.  I am trying to do this in C# with Visual Studio 8.

Here is a sample of the XML file:

<?xml version="1.0" encoding="utf-8"?>
<Books>
  <Book>
    <Title>Programming with C</Title>
    <Author>Author 1</Author>
    <Publisher>Publisher 1</Publisher>
  </Book>
  <Book>
    <Title>Working with C</Title>
    <Author>Author 2</Author>
    <Publisher>Publisher 2</Publisher>
  </Book>
  <Book>
    <Title>How to program C</Title>
    <Author>Author 3</Author>
    <Publisher>Publisher 3</Publisher>
  </Book>
</Books>

What I want to do is to search for elements whose Title contains the substring "program".  Below is the sample code I tried to use but did not work:
=======================================================================
using System.Linq;
using System.Xml.Linq;

XElement doc = XElement.Load("C:\\ProgrammingBooks.xml");
var ProgrammingBooks = from row in doc.Elements("Book")
                         where ((string)row.Element("Title")).ToUpper().Contains("PROGRAM")
                         select row;
MessageBox.Show("Matched Books: " + ProgrammingBooks.Count().ToString());
=======================================================================
When I check the results I always end up with an empty result so I guess the where clause is not working.  If I take the where clause out then I get all the rows. I suppose I could doe the filtering in a subsequent ForEach loop but I would like to see first if I can get the where clause to work.  The end result should be 2 rows that match which are element 1 and 3 from the file.

Thanks in advance.
0
Comment
Question by:zonkerman
  • 2
2 Comments
 
LVL 9

Expert Comment

by:Sreedhar Vengala
ID: 24082003
Try this:

from row in doc.Descendants("Book") where ((string)row.Element("Title")).ToUpper().Contains("PROGRAM") select row;
0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 500 total points
ID: 24082037
XElement doc = XElement.Load("C:\\ProgrammingBooks.xml");
            IEnumerable<XElement> elements = from row in doc.Descendants("Book") where ((string)row.Element("Title")).ToUpper().Contains("PROGRAM") select row;

            foreach (var element in elements)
            {
                Console.Write(element);
            }
            Console.ReadKey();

Output: as in attached .jpg

output.jpg
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Code works but I need to redo it and assign values to Model 7 33
Syntax error 9 45
Angular JS Route 3 54
Interview questions for support of a legacy ASP.NET site 4 39
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

16 Experts available now in Live!

Get 1:1 Help Now