0

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!

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • Using `Elements("name")` should be absolutely fine. If it isn't, please show a short but *complete* example (a complete XML document and a complete console app) showing it not working. – Jon Skeet Mar 03 '16 at 08:42
  • This still isn't a complete program. We should be able to copy/paste/compile/run. Please read http://tinyurl.com/stack-hints – Jon Skeet Mar 03 '16 at 09:04
  • I'm so sorry, I'll do that next time. Your answer worked though, thanks! –  Mar 03 '16 at 09:07

2 Answers2

1

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();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I tried it, and I got this result: http://imgur.com/3QdFctc, Also this is my XML file: http://imgur.com/VzJhax0 –  Mar 03 '16 at 08:58
  • @Bas: Screenshots are a very hard to use way of sharing your problem. Please update your question with a short but complete example of code, XML and results *as text*. Note that the ancestor elements of the XML could be very important, due to namespace defaulting. – Jon Skeet Mar 03 '16 at 08:59
  • @Bas: See my edit - we were looking for name directly in `tracks`, not `track`... – Jon Skeet Mar 03 '16 at 09:02
  • @Bas: No, you still haven't edited the question. And it would be more useful to include a small but complete sample of the XML *in the question*. But I *have* edited the answer, so you should be able to use that. – Jon Skeet Mar 03 '16 at 09:03
  • Your answer worked! I'm terribly sorry for being unclear, I'll keep everything you said in mind for future question. Thanks a million for your help! –  Mar 03 '16 at 09:06
0

You can try excluding with a condition, like myXMLDoc. ... .Descendants("name").Where(x => x.Parent.Name != "artist").

mylkee
  • 1
  • 1