Solved

LINQ to XML Use Of Contains For String Compare In Where

Posted on 2009-04-06
2
840 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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

758 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

21 Experts available now in Live!

Get 1:1 Help Now