0

I'm developing an application that queries MusicBrainz for data and I'm viewing it by binding the results to ListViews using some XPath.

enter image description here

Now, the underlying XML for the second (albums) ListView is here, and as you can see the top result has two artists:

<metadata created="2013-05-10T21:32:13.487Z">
    <release-group-list count="153471" offset="0">
        <release-group id="22315cdd-4ed9-427c-9492-560cf4afed58" type="Album" ext:score="100">
            <title>The Heist</title>
            <primary-type>Album</primary-type>
            <artist-credit>
                <name-credit joinphrase=" & ">
                    <artist id="b6d7ec94-830c-44dd-b699-ce66556b7e55">
                        <name>Macklemore</name>
                        <sort-name>Macklemore</sort-name>
                    </artist>
                </name-credit>
                <name-credit>
                    <artist id="c01560d1-6f69-48cf-a3c6-c94b65f099b1">
                        <name>Ryan Lewis</name>
                        <sort-name>Lewis, Ryan</sort-name>
                    </artist>
                </name-credit>
            </artist-credit>

but using this code

View.SetBinding(ListView.ItemsSourceProperty, new Binding()
{
    Source = Resources["DataProvider"],
    XPath = "//a:metadata/a:release-group-list/a:release-group"
});

GridView.Columns.Add(new GridViewColumn()
{
    DisplayMemberBinding = new Binding() { XPath = "a:artist-credit/a:name-credit/a:artist/a:name" },
    Header = "Artist",
    Width = 128
});

I only get the first result and I have no idea how to go about concatenating them.

Any insight will be greatly appreciated.

4

1 に答える 1

1

Linq-to-Xml 経由で話しているデータを取得する方法は次のとおりです。

public class XmlArtistsConcept
{
    public void Run()
    {
        XDocument artistDocument = XDocument.Load(@"http://musicbrainz.org/ws/2/release-group?query=the%20heist");
        XNamespace artistNamespace = @"http://musicbrainz.org/ns/mmd-2.0#";

        // The purpose of this query is to demonstrate getting this for a particular result.                
        var theHeistNames =
            string.Join(", ",
                artistDocument
                .Element(artistNamespace + "metadata")
                .Element(artistNamespace + "release-group-list")
                .Elements(artistNamespace + "release-group")
                .Where(element => element.Attribute("id").Value == "22315cdd-4ed9-427c-9492-560cf4afed58").Single()
                .Elements(artistNamespace + "artist-credit")
                .Elements(artistNamespace + "name-credit")
                .Elements(artistNamespace + "artist")
                .Select(artist => artist.Element(artistNamespace + "name").Value).ToArray());

        Console.WriteLine(theHeistNames);

        // This query will get it for everything in the XDocument. I made a quick data bucket to dump the values in.
        var allAlbumResults =
            artistDocument
            .Element(artistNamespace + "metadata")
            .Element(artistNamespace + "release-group-list")
            .Elements(artistNamespace + "release-group")
            .Where(releaseGroup => releaseGroup.Attribute("type") != null)
            .Select(releaseGroup =>
            {
                return new AlbumResult()
                {
                    Title = releaseGroup.Element(artistNamespace + "title").Value,
                    Artist = string.Join(", ",
                                    releaseGroup
                                    .Elements(artistNamespace + "artist-credit")
                                    .Elements(artistNamespace + "name-credit")
                                    .Elements(artistNamespace + "artist")
                                    .Select(artist => artist.Element(artistNamespace + "name").Value)
                                    .ToArray()),
                    Type = releaseGroup.Attribute("type").Value,
                };
            });

        allAlbumResults.ToList().ForEach(albumResult => Console.WriteLine("Title: {0}, Artist: {1}, Type: {2}", albumResult.Title, albumResult.Artist, albumResult.Type));
        Console.WriteLine();
        Console.WriteLine("Finished");
    }
}

public class AlbumResult
{
    public string Title { get; set; }
    public string Artist { get; set; }
    public string Type { get; set; }
}
于 2013-05-11T04:02:37.160 に答える