Parsing XML excluding elements

I'm trying to parse a xml file, which isn't the problem. But my xml file has multiple <name> tags. One is for a song, and the other one is for an artist. I want to exclude the artist name tag, but I can't figure out how.

Here's my code so far:

        string xmlString;
        using (WebClient wc = new WebClient())
        {
            xmlString = wc.DownloadString(@"http://ws.audioscrobbler.com/2.0/?method=album.getInfo&album=awake&artist=Dream%20Theater&api_key=b5cbf8dcef4c6acfc5698f8709841949");
        }
        List<XElement> alleElements = new List<XElement>();
        XDocument myXMLDoc = XDocument.Parse(xmlString);
        List<XElement> trackNames = myXMLDoc.Descendants("album")
                           .Elements("tracks")
                           .Elements("name")
                           .ToList();


        foreach (XElement el in trackNames)
        {
            Console.WriteLine(el);
        }

        Console.WriteLine("-----End-----");
        Console.ReadLine();

I tried using .Elements("name"); instead of Descendants, but that returns nothing at all.

Here's a small part of my xml file:

<track rank="1">
    <name>6:00</name>
    <url>http://www.last.fm/music/Dream+Theater/_/6:00</url>
    <duration>331</duration>
    <streamable fulltrack="0">0</streamable>
    <artist>
        <name>Dream Theater</name>
        <mbid>28503ab7-8bf2-4666-a7bd-2644bfc7cb1d</mbid>
        <url>http://www.last.fm/music/Dream+Theater</url>
    </artist>
</track>

Is there a way to exclude the <name> tag inside the <artist> tag.

If I'm not clear enough, please let me know and I'll explain it more!

Jon Skeet
people
quotationmark

It sounds like you just want to use Elements instead of Descendants, but at the right point - it's not clear where you tried using it. I'd also recommend using ToList to make things simpler. Given the sample in the documentation, it looks like this would work and be clearer in terms of where we expect there to be multiple elements:

List<XElement> trackNames = doc.Root
                               .Element("tracks")
                               .Elements("track")
                               .Elements("name")
                               .ToList();

people

See more on this question at Stackoverflow