Daan
Daan

Reputation: 1437

Extract a list of certain elements from XML

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.

Upvotes: 0

Views: 2939

Answers (4)

remarkrm
remarkrm

Reputation: 159

Although LINQ is certainly a valid approach, I figured I would mention at least one quick alternative: XPath. Here is an example:

XPathDocument doc = new XPathDocument("http://api.discogs.com/release/368116?f=xml");
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter = (XPathNodeIterator)nav.Evaluate("//tracklist/track/title");

while (iter.MoveNext())
{
    Console.WriteLine(iter.Current.Value);
}

Output is as follows:

Planet Telex
The Bends
High And Dry
Fake Plastic Trees
Bones
(Nice Dream)
Just
My Iron Lung
Bullet Proof..I Wish I Was
Black Star
Sulk
Street Spirit (Fade Out)

Note that I added ?f=xml on your sample URL, since the default output from the API is JSON.

Upvotes: 0

Daniel Hilgarth
Daniel Hilgarth

Reputation: 174397

If you can, go with LINQ to XML:

XDocument doc = XDocument.Load(xml);
var titles = doc.Descendants("title").Select(x => x.Value);

A more sophisticated version that distinguishes between the album and the track title is the following:

var titles = doc.Descendants("release")
                .Select(x => new
                             {
                                 AlbumTitle = x.Element("title").Value,
                                 Tracks = x.Element("tracklist")
                                           .Descendants("title")
                                           .Select(y => y.Value)
                             });

It returns a list of anonymous types, each with a property AlbumTitle of type string and an IEnumerable<string> representing the track titles.

Upvotes: 3

Bassam Mehanni
Bassam Mehanni

Reputation: 14944

Check out this simpleXml library

https://bitbucket.org/kberridge/simplexml

It's on NuGet by the way!

Install-package simpleXml

Upvotes: 0

Daniel Mann
Daniel Mann

Reputation: 59035

Use xsd.exe to generate a class structure from your XML file, then deserialize your XML into that class structure. It should be pretty straightforward.

Upvotes: 0

Related Questions