2012-07-18 69 views
0

爲什麼這一工作是這樣的:爲什麼這段代碼不能工作? XML與.StartsWith

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

我想用遊戲ID,而不是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; } } 
} 
+0

是GameID的一個字符串? – 2012-07-18 10:29:21

+0

-1:沒有XML提供 – 2012-07-18 10:29:29

+0

你還沒有說過它不工作的方式。 – 2012-07-18 10:29:36

回答

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

,它應該工作的罰款。

+0

謝謝。它效果很好。你們是最棒的。 – iamrelos 2012-07-18 10:45:06

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

是的,我得到一個NullReferenceException,但eveything似乎是正確的。 – iamrelos 2012-07-18 10:40:20

+0

@ Twenty40 press f5 ...我已經解釋過了。有很多沒有ID/GameID的記錄 – 2012-07-18 10:40:51