Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

LINQ to XML Use Of Contains For String Compare In Where

Posted on 2009-04-06
2
Medium Priority
?
848 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
[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
  • 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 2000 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 SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

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!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…

721 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