我在XDocument中有以下XML需要解析:
<RESPONSES>
<RESPONSE STATUS="OK">
<ALBUM>
<GN_ID>63074689-EDADA0FEDE93683CA03C6D38520A4D88</GN_ID>
<ARTIST>Green Day</ARTIST>
<TITLE>American Idiot</TITLE>
<PKG_LANG>ENG</PKG_LANG>
<DATE>2004</DATE>
<GENRE NUM="105222" ID="35474">Punk</GENRE>
<TRACK_COUNT>13</TRACK_COUNT>
<TRACK>
<TRACK_NUM>1</TRACK_NUM>
<GN_ID>63074690-456E41C113DC8354DC6B25421F2C7989</GN_ID>
<TITLE>American Idiot</TITLE>
</TRACK>
<TRACK>
<TRACK_NUM>2</TRACK_NUM>
<GN_ID>63074691-70EFB1E8EB31B5296D5822E55343EFA9</GN_ID>
<TITLE>Jesus Of Suburbia / City Of The Damned / I Don't Care / Dearly Beloved / Tales Of Another Broken Home</TITLE>
</TRACK>
<URL TYPE="COVERART" SIZE="THUMBNAIL" WIDTH="75" HEIGHT="75">http://akamai-b.cdn.cddbp.net/cds/2.0/cover/0A1A/BABF/DEBC/CF21_thumbnail_front.jpg</URL>
<URL TYPE="ARTIST_BIOGRAPHY">http://web.content.cddbp.net/cds/2.0?id=E1DA4CCF2BBE645C&client=10395648&class=biography&type=text/plain&tag=02vq7D8a8HkrfJ4rWglsq09xYTY0H7Fb.YOadB73lz1-z3EWy.dWzCVA</URL>
</ALBUM>
</RESPONSE>
</RESPONSES>
我可以根据标签判断曲目的数量,但除了曲目数量因专辑而异。这是我现有的用于解析XDocument的C#代码:
var albums = new List<Album>();
try
{
albums = (from item in inputDoc.Descendants("ALBUM") //.Elements("GN_ID")
select new Album
{
AlbumId = (string)item.Element("GN_ID"),
ArtistName = (string)item.Element("ARTIST"),
AlbumName = (string)item.Element("TITLE"),
TrackCount = (int)item.Element("TRACK_COUNT"),
Year = (string)item.Element("DATE"),
Genre = (string)item.Element("GENRE"),
CoverArt = item.Elements("URL").First(u => u.Attribute("TYPE").Value == "COVERART").Value.ToString(),
Biography = item.Elements("URL").First(u => u.Attribute("TYPE").Value == "ARTIST_BIOGRAPHY").Value.ToString()
}).ToList();
}
这是我正在阅读的结构:
namespace MusicApp.Model
{
public class Album
{
public string ArtistName { get; set; }
public string AlbumId { get; set; }
public string AlbumName { get; set; }
public int TrackCount { get; set; }
public string Year { get; set; }
public string Genre { get; set; }
public string CoverArt { get; set; }
public string Biography { get; set; }
// Navigation properties
public ICollection<Track> Tracks { get; set; }
}
public class Track
{
public string TrackId { get; set; }
public int TrackNumber { get; set; }
public string TrackName { get; set; }
public string AlbumId { get; set; }
}
}
但是我不知道如何解析曲目(假设没有事先知道在给定的回复中会有多少。有人可以帮我解决这个问题吗?
哦 - 当我在它的时候,我该如何转换“Ampersand-'s”?到一个撇号(除了string.replace())。
提前致谢!
答案 0 :(得分:2)
添加
Tracks = item.Elements("TRACK")
.Select(t=> new Track{
TrackNumber = (int)t.Element("TRACK_NUM"),
TrackName = (string)t.Element("TITLE"),
TrackId = (string)t.Element("GN_ID"),
}).ToList()
代码......
所以整个代码将是
var albums = (from item in inputDoc.Descendants("ALBUM")
select new Album
{
AlbumId = (string)item.Element("GN_ID"),
ArtistName = (string)item.Element("ARTIST"),
AlbumName = (string)item.Element("TITLE"),
TrackCount = (int)item.Element("TRACK_COUNT"),
Year = (string)item.Element("DATE"),
Genre = (string)item.Element("GENRE"),
CoverArt = item.Elements("URL").First(u => u.Attribute("TYPE").Value == "COVERART").Value.ToString(),
Biography = item.Elements("URL").First(u => u.Attribute("TYPE").Value == "ARTIST_BIOGRAPHY").Value.ToString(),
Tracks = item.Elements("TRACK")
.Select(t=> new Track{
TrackNumber = (int)t.Element("TRACK_NUM"),
TrackName = (string)t.Element("TITLE"),
TrackId = (string)t.Element("GN_ID"),
}).ToList()
}).ToList();