C#XDocument从特定元素获取属性

时间:2014-04-09 09:53:53

标签: c# xml windows-phone-8 nodes

我有一个问题,即我试图从元素中读取特定属性,但我无法从中获取值。我试图得到的值是拇指=" boxart / thumb / original / front / 2-1.jpg"来自这个元素:

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

我希望有人能提供帮助。感谢。

4 个答案:

答案 0 :(得分: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);

答案 1 :(得分: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));
}

答案 2 :(得分:0)

'boxart'元素是'Images'的孩子'GameTile'等是'Game'的孩子。当你应该从“图像”中获取它们时,看起来你试图从游戏中获得一个盒子。

结帐你的'x'会员。

答案 3 :(得分:0)

你可以像这样获得boxart元素的所有拇指属性:

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