0

これがこのように機能する理由:

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

GameTitleの代わりにGameIDを使用したい。

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; } }
}
4

2 に答える 2

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

そしてそれはうまくいくはずです。

于 2012-07-18T10:38:24.080 に答える
0

データがないことを考えると、ここでの唯一の重要な違いは.GameTitle.vs.GameID.です。ここでの「動作しない」とは、「例外がスローされる」ことを意味すると想定しています。私が予測できる主NullReferenceExceptionな例外.GameIDはです。null.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"));
于 2012-07-18T10:30:44.817 に答える