LINQ to XML Use Of Contains For String Compare In Where
Posted on 2009-04-06
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"?>
<Title>Programming with C</Title>
<Title>Working with C</Title>
<Title>How to program C</Title>
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:
XElement doc = XElement.Load("C:\\ProgrammingBooks.xml");
var ProgrammingBooks = from row in doc.Elements("Book")
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.