为什么这段代码不起作用?带有.StartsWith的XML

时间:2012-07-18 10:25:16

标签: c# xml windows-phone-7 linq-to-xml

为什么这样的工作如下:

        XDocument dataFeed = XDocument.Parse(e.Result);
        var guide = from query in dataFeed.Descendants("MaxPayne3")
                                    select new NewGamesClass
                                    {
                                        GameID = (string)query.Element("ID"),
                                        GameTitle = (string)query.Element("Title"),
                                        GameDescription = (string)query.Element("Description"),
                                        GameGuide = (string)query.Element("Guide")
                                    };

        if (NavigationContext.QueryString.TryGetValue("selectedItem", out selectedIndex))
        {
            if (selectedIndex == "0")
                GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Feel"));
            else if (selectedIndex == "1")
                GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameTitle.StartsWith("Serious"));

但不是这样的:

            if (selectedIndex == "0")
                GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("000"));
            else if (selectedIndex == "1")
                GuidesListBox.ItemsSource = guide.Where(ngc => ngc.GameID.StartsWith("001"));

我想使用GameID而不是GameTitle。

很抱歉没有XML,对我来说太早了,哈哈,

下面: https://dl.dropbox.com/u/27136243/AchivementHunters/XML/GameList.xml

这是班级:

public class NewGamesClass
{
    string gameID;
    string gameTitle;
    string gamedescription;
    string gameImage;
    string gameGuide;
    string videoLink;
    public string GameID
    { get { return gameID; } set { gameID = value; } }
    public string GameTitle
    { get { return gameTitle; } set { gameTitle = value; } }
    public string GameDescription
    { get { return gamedescription; } set { gamedescription = value; } }
    public string GameImage
    { get { return gameImage; } set { gameImage = value; } }
    public string GameGuide
    { get { return gameGuide; } set { gameGuide = value; } }
    public string VideoLink
    { get { return videoLink; } set { videoLink = value; } }
}

2 个答案:

答案 0 :(得分:2)

大多数<MaxPayne3>节点没有子<ID>节点,因此这段代码:

select new NewGamesClass
           {
               GameID = (string)query.Element("ID"),
               GameTitle = (string)query.Element("Title"),
               GameDescription = (string)query.Element("Description"),
               GameGuide = (string)query.Element("Guide")
            }

将主要为GameID生成空值。然后这段代码:

guide.Where(ngc => ngc.GameID.StartsWith("000"))

将为所有这些元素抛出NullReferenceException

将其更改为:

guide.Where(ngc => !String.IsNullOrEmpty(ngc.GameID) && ngc.GameID.StartsWith("000"))

它应该可以正常工作。

答案 1 :(得分:0)

鉴于我们没有数据,这里唯一重要的区别是.GameTitle. vs .GameID.。我假设“不会工作”这里意味着“它会引发异常”。我可以预测的主要例外是NullReferenceException,因为.GameIDnull,所以.GameID.anything是非法的。

这让我觉得<ID>whatever</ID>在您认为的位置不存在(这会导致.GameID成为null引用)。要验证的事情:

  • 它存在
  • 案例正确(IDIdid等)
  • 元素,而不是属性
  • 它不在命名空间

更新:这是数据中的问题:

<MaxPayne3>
  <Title>Part I Complete - 20G</Title>
  <Description>Complete Part I Of The Story</Description>
  <Guide>Story related, cannot be missed.</Guide>
  <Image>http://www.xbox360achievements.org/images/achievements/925/-K6lMA==.jpg</Image>
  <VideoLink/>
</MaxPayne3>

没有<ID>。还有很多这样的事情。所以:你需要处理它。务实的检查将是:

...blah...guide.Where(ngc => ngc.GameID != null && ngc.GameID.StartsWith("000"));
相关问题