0

I need to parse multiple xml files with songs info in them. The structure is something like this:

<song_info>
 <title>Title1</title>
 <artist>Artist1</artist>
 <lyrics>Sample lyrics</lyrics>
</song_info>

The user enters a phrase and I need to search for the phrase in the lyrics tag. If the phrase exists in the lyrics tag, I need to output a link to the song file. How is this done with LINQ? I am using .NET 3.5 . Thanks!

2 Answers 2

2

If each file just contains a single song info you could just do

XDocument xdoc = XDocument.Load("somefile.xml");
string phrase ="lyric";
if(xdoc.Descendants("lyrics").First().Value.Contains(phrase))
{
   //file contains phrase
}
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, each xml file contains 1 song!
0
string keyword = "dummy";
List<string> files = new List<string>();
foreach (var file in Directory.GetFiles(@"d:\lyrics"))
{
    XmlDocument doc = new XmlDocument();
    doc.Load(file);
    if (doc.SelectSingleNode("/song_info/lyrics").InnerText.Contains(keyword))
    {
        files.Add(file);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.