0

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.

1 Answers1

1

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.

tech-gayan
  • 1,373
  • 1
  • 10
  • 25
  • What if the string would span over multiple "line" elements containing its own "word" elements? And what if i wanted to access attributes of the first "word" element that contain part of the string? – Adam Kotvas Jul 01 '20 at 08:13