0

我正在开发一个应用程序来查询 MusicBrainz 的数据,并通过使用一些 XPath 将结果绑定到 ListViews 来查看它。

在此处输入图像描述

现在,第二个(专辑)ListView 的底层 XML 在这里,您可以看到顶部结果有两个艺术家:

<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>

但使用此代码

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
});

我只得到第一个结果,我不知道如何连接它们。

任何见解将不胜感激。

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 回答