user1502412
user1502412

Reputation: 33

How to parse a XML file using Linq - descendants

I am having trouble using XDocument Linq-to-XML in C# to parse the following XML:

<matches count="9">
<books count="4">
    <book>
        <key>books/batman_dark_knight_returns/frank_miller</key>
        <title>Batman: The Dark Knight Returns</title>
    </book>
    ...
</books>
<movies count="4">
    <movie>
        <key>movies/batman_tales_of_dark_knight/boy_kirklan</key>
        <title>Batman: Tales of the Dark Knight</title>
    </movie>
    ...
</movies>
<music count="1">
    <album>
        <key>music/dark_knight/hans_zimmer</key>
        <title>The Dark Knight</title>
    </album>
</music> </matches>

A snippet of my very very wrong code is

var data = from query in xmlFeed.Descendants("books")
           select new Book
           {
               Title = (string)query.Element("book").Element("title"),

               count = (string)query.Element("stickers").Element("sticker").Element("count")

           };

searchListBox.ItemsSource = data;

Any help on how I can parse a xml like this that returns books, movies, tv shows in a long list that I can bind to a list box?

Thanks

Upvotes: 2

Views: 933

Answers (2)

Chuck Savage
Chuck Savage

Reputation: 11955

If all key's are followed by the title node:

var all = xmlFeed.Descendants("key").Select(x => new {
          Key = x.Value,
          Title = ((XElement)x.NextNode).Value
          // else the Title would be
          // Title = x.Parent.Element("title").Value
          }).ToList();

This gets all the Key's and Titles as a long list.

Upvotes: 0

Jon Skeet
Jon Skeet

Reputation: 1500525

I suspect you want something like:

var books = xmlFeed.Descendants("book").Select(x => Book.FromXElement(x));
var movies = xmlFeed.Descendants("movie").Select(x => Movie.FromXElement(x));
var albums = xmlFeed.Descendants("album").Select(x => Album.FromXElement(x));

// I'm assuming you're using .NET 4 here, otherwise it becomes slightly uglier.
var allResults = books.Concat<object>(movies).Concat(albums).ToList();

That's assuming you add a static FromXElement method to each of your classes, which I've generally found to be a clean way of handling this sort of thing. You can do it inline if you want, but it makes the query code get bigger and bigger as you start fetching more bits.

Upvotes: 4

Related Questions