[英]How to concatenate multiple XML childnodes into one in a ListView row?
I'm developing an application that queries MusicBrainz for data and I'm viewing it by binding the results to ListViews using some XPath. 我正在开发一个查询MusicBrainz的数据的应用程序,并通过使用一些XPath将结果绑定到ListViews来查看它。
Now, the underlying XML for the second (albums) ListView is here , and as you can see the top result has two artists: 现在,第二个(相册)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>
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. 任何见识将不胜感激。
Here is a way to get the data that you're talking about via Linq-to-Xml: 这是一种通过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; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.