Why this code wont work? XML with .StartsWith

Why this work like this:

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

But not like this:

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

i want to use the GameID Instead The GameTitle.

Sorry about no XML, too early in the morning for me, lol

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

Here is the Class:

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

Answers


Most of the <MaxPayne3> nodes do not have a child <ID> node, hence this code:

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

will produce mostly nulls for GameID. Then this code:

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

will throw NullReferenceException for all those elements

change it to this:

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

and it should work fine.


Given that we have no data, the only significant difference here is the .GameTitle. vs .GameID.. I'm assuming that "won't work" here means "it throws an exception". The primary exception I can predict there would be NullReferenceException, because .GameID is null, so .GameID.anything is illegal.

Which makes me think that <ID>whatever</ID> doesn't exist at the location you think it does (which will result in .GameID being a null reference). Things to verify:

  • that it exists there
  • that the case is correct (ID, Id, id, etc)
  • that is is an element, not an attribute
  • that it isn't in a namespace

Update: and here's the problem from the data:

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

which has no <ID>. There are a lot more like this. So: you need to handle that. A pragmatic check would be:

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

Need Your Help

How to get the name of the module, calling a specific function, in Python.

python function module directory operating-system

Is there a way to find out which file is calling a function? (similar to os.path.basename(__file__))

Enable PhpMyAdmin’s Extra Features

php mysql phpmyadmin

I am encountering a problem enabling the extra features in phpmyadmin.