Matching a string with XML with value stored over multiple elements

I want to find out if some string has a match in an XML file.

for example:

string test="New York, USA"; 

in an XML file formated this way:

<?xml version="1.0"?> <line>     <word>New</word>     <word>York,</word>     <word>USA</word> </line> 

so that every word may or may not be in a different element

What is the most easiest way to do this? I was thinking about matching each word separately while saving the partial results but it seems to me like there have to be easier way.

Add Comment
1 Answer(s)

If you want to compare word by word you can compare this using two string lists. using below you can get xml to List

List<string> list = doc.Root.Descendants("line").Descendants()                     .Select(element => element.Value)                     .ToList(); 

Then take your comparison string to list

string words = "New York, USA"; List<string> result = words.Split(' ').ToList(); 

Compare both lists using Intersect(). refer this

 var matcheditems = list.Intersect(result); 

Hope this will help you.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.