Solved

LINQ to XML Use Of Contains For String Compare In Where

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

772 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