2014-04-09 33 views
0

我有一個問題,即時通訊嘗試從元素中讀取特定屬性,但我無法從中獲取該值。該值Im試圖讓從這個元素拇指= 「boxart /拇指/原廠/正面/ 2-1.jpg」:C#XDocument從特定元素獲取屬性

<boxart side="front" width="1525" height="2160" thumb="boxart/thumb/original/front/2-1.jpg">boxart/original/front/2-1.jpg</boxart> 

XML:

<Data> 
    <baseImgUrl>http://thegamesdb.net/banners/</baseImgUrl> 
    <Game> 
    <id>2</id> 
    <GameTitle>Crysis</GameTitle> 
    <PlatformId>1</PlatformId> 
    <Platform>PC</Platform> 
    <ReleaseDate>11/13/2007</ReleaseDate> 
    <Overview> 
    From the makers of Far Cry, Crysis offers FPS fans the best-looking, most highly-  evolving gameplay, requiring the player to use adaptive tactics and total customization of weapons and armor to survive in dynamic, hostile environments including Zero-G. Earth, 2019. A team of US scientists makes a frightening discovery on an island in the South China Sea. All contact with the team is lost when the North Korean Government quickly seals off the area. The United States responds by dispatching an elite team of Delta Force Operators to recon the situation. As tension rises between the two nations, a massive alien ship reveals itself in the middle of the island. The ship generates an immense force sphere that freezes a vast portion of the island and drastically alters the global weather system. Now the US and North Korea must join forces to battle the alien menace. With hope rapidly fading, you must fight epic battles through tropical jungle, frozen landscapes, and finally into the heart of the alien ship itself for the ultimate Zero G showdown. 
    </Overview> 
<ESRB>M - Mature</ESRB> 
<Genres> 
<genre>Shooter</genre> 
</Genres> 
<Players>4+</Players> 
<Co-op>No</Co-op> 
<Youtube>http://www.youtube.com/watch?v=i3vO01xQ-DM</Youtube> 
<Publisher>Electronic Arts</Publisher> 
<Developer>Crytek</Developer> 
<Rating>8.1111</Rating> 
<Images> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-1.jpg</original> 
    <thumb>fanart/thumb/2-1.jpg</thumb> 
    </fanart> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-2.jpg</original> 
    <thumb>fanart/thumb/2-2.jpg</thumb> 
    </fanart> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-3.jpg</original> 
    <thumb>fanart/thumb/2-3.jpg</thumb> 
    </fanart> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-4.jpg</original> 
    <thumb>fanart/thumb/2-4.jpg</thumb> 
    </fanart> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-5.jpg</original> 
    <thumb>fanart/thumb/2-5.jpg</thumb> 
    </fanart> 
    <fanart> 
    <original width="1920" height="1080">fanart/original/2-6.jpg</original> 
    <thumb>fanart/thumb/2-6.jpg</thumb> 
    </fanart> 
    <boxart side="back" width="1525" height="2162" thumb="boxart/thumb/original/back/2-1.jpg">boxart/original/back/2-1.jpg</boxart> 
    <boxart side="front" width="1525" height="2160" thumb="boxart/thumb/original/front/2-1.jpg">boxart/original/front/2-1.jpg</boxart> 
    <banner width="760" height="140">graphical/2-g2.jpg</banner> 
    <banner width="760" height="140">graphical/2-g3.jpg</banner> 
    <screenshot> 
    <original width="1920" height="1080">screenshots/2-1.jpg</original> 
    <thumb>screenshots/thumb/2-1.jpg</thumb> 
    </screenshot> 
    <clearlogo width="400" height="95">clearlogo/2.png</clearlogo> 
</Images> 
</Game> 
</Data> 

這裏是我的代碼即時通訊使用閱讀使用XML:

var feedXml = XDocument.Parse(e.Result); 

      var gameData = feedXml.Root.Descendants("Game").Select(x => new GetGame 
      { 
       ID = (int)x.Element("id"), 
       GameTitle = (string)x.Element("GameTitle"), 
       Platform = (string)x.Element("Platform"), 
       ReleaseDate = (string)x.Element("ReleaseDate"), 
       Overview = (string)x.Element("Overview"), 
       ESRB = (string)x.Element("ESRB"), 
       BoxArt = new Uri((string)x.Element("boxart")), // This is where i would like to read the image thumb from. 
      }) 
       .ToList(); 
      foreach (var item in gameData) GetGameItems.Add(item); 

我希望有一個人能提供幫助。謝謝。

回答

1

因爲它可以得到一個有點冗長,我建議定義了一種新方法:

private static string GetBoxArt(XElement gameNode) 
{ 
    return (string)gameNode.Descendants("boxart") 
          .FirstOrDefault(b => (string)b.Attribute("side") == "front"); 
} 

然後使用:

BoxArt = new Uri(GetBoxArt(x)), 

在您的原始代碼。

而且這裏是你可以用它來獲得一個逗號分隔的所有流派的列表的方法:

private static string GetGenres(XElement gameNode) 
{ 
    return string.Join(", ", gameNode.Descendants("genre").Select(g => (string)g)); 
} 
+0

謝謝它工作正常:)。 – Thunder

+0

您是否知道爲什麼.FirstOrDefault會比.First更慢? – Thunder

+0

我想'FirstOrDefault'比'First'包含更多的處理,因爲它必須做額外的事情,我會認爲大多數情況下,會有明顯的效果。你確定有區別嗎?如果您的數據以相同的速度加載,但只是圖像看起來較慢,那麼與FirstOrDefault和First相關無關。 – JLRishe

0

'boxart'元素是'圖片'的子元素,'GameTile'等是'遊戲'的子元素。它看起來像你試圖從遊戲中獲得boxart,當你應該從'圖像'中獲得它們。

結帳您的'x'會員。

1

既然有兩個盒子藝術,你需要在查詢中告訴你需要哪一個。舉例來說,如果你要檢索的箱體前藝術:

var feedXml = XDocument.Parse(e.Result); 

var gameData = feedXml.Root.Descendants("Game").Select(x => new GetGame 
{ 
    ID = (int)x.Element("id"), 
    GameTitle = (string)x.Element("GameTitle"), 
    Platform = (string)x.Element("Platform"), 
    ReleaseDate = (string)x.Element("ReleaseDate"), 
    Overview = (string)x.Element("Overview"), 
    ESRB = (string)x.Element("ESRB"), 
    BoxArt = new Uri((string)x.Element("Images").Elements("boxart").First(e => (string)e.Attribute("side") == "front").Attribute("thumb")), 
}) 
    .ToList(); 
foreach (var item in gameData) GetGameItems.Add(item); 
+0

當我試試這個,我得到這個錯誤:無效的URI:URI的格式無法確定。 – Thunder

+0

沒關係,它工作正常:)。謝謝!。順便說一句, – Thunder

+0

。你知道我如何檢查元素是否存在。例如,boxart前面的元素並不總是存在,並且它不會在我的應用程序崩潰。 – Thunder

0

你可以得到這樣boxart元素的所有拇指屬性:

var attributes = feedXml.Descendants("boxart").Attributes().ToList().Where(attribute => attribute.Name.LocalName.Equals("thumb")).ToList();