2014-02-06 77 views
0

我有下面的XML這是在一個XDocument,我需要解析:需要讀取一個XDocument重複值

<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&apos;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&amp;client=10395648&amp;class=biography&amp;type=text/plain&amp;tag=02vq7D8a8HkrfJ4rWglsq09xYTY0H7Fb.YOadB73lz1-z3EWy.dWzCVA</URL> 
    </ALBUM> 
    </RESPONSE> 
</RESPONSES> 

我可以告訴基於標籤的磁道數,但其他比曲目數量因專輯而異。這裏是我現有的C#代碼來解析的XDocument:

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」轉換爲一個撇號(除了string.replace())。

提前致謝!

回答

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(); 
+0

謝謝LB - 那個偉大工程!剩下的一個問題:我如何填充Tracks中的AlbumId(這是一個外鍵返回到Album-> AlbumId?哦 - 當我在它的時候,我如何將「Ampersand -'I」轉換爲撇號除了string.replace())。 –

+0

@ user3171260 a)你已經做了'(string)item.Element(「GN_ID」)',只需在* Track block *中重複。 b)對於'''你不必做任何特別的事情。 XML解析器可以處理它。 –