I’m trying to create a C# application that extracts data from pages like this one. It’s basically an XML file that stores information about a music album. Here’s the relevant code:
<resp stat="ok" version="2.0">
<release id="368116" status="Accepted">
<title>The Bends</title>
<tracklist>
<track>
<position>1</position>
<title>Planet Telex</title>
<duration>4:18</duration>
</track>
</tracklist>
</release>
I’d like to extract all the track titles from the album (in the above code “Planet Telex”) and output them in a list like this:
Planet Telex
The Bends
...
what would be the best/most elegant way to do this? From what I’ve read, the XmlTextReader is a good class to use. I’ve also seen many mentions of Linq to XML… Thanks in advance!
BTW, I’ve posted this question again (albeit formulated differently). I’m not sure why it was removed last time.
If you can, go with LINQ to XML:
A more sophisticated version that distinguishes between the album and the track title is the following:
It returns a list of anonymous types, each with a property
AlbumTitleof typestringand anIEnumerable<string>representing the track titles.