2013-05-10 61 views
0

我正在开发一个查询MusicBrainz数据的应用程序,我正在使用一些XPath将结果绑定到ListViews来查看它。如何将多个XML子节点连接成ListView行中的一个?

enter image description here

现在,底层的XML第二(专辑)的ListView是here,正如你可以看到上面的结果有两个艺术家:

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

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

任何有识之士将不胜感激。

+0

你必须把它绑定到一个XPath?你可以使用Linq-to-Xml来创建一个绑定的集合吗? – 2013-05-11 03:04:10

+0

当然,它不一定是XPath,这正是我现在使用的。感谢您的建议,我会尽快研究它。 – Kuraj 2013-05-11 04:00:02

回答

1

这里是一个办法让你通过LINQ到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; } 
} 
+0

是的!非常感谢你! – Kuraj 2013-05-11 10:40:22

相关问题