0

I'm trying to read an xml file using xmlTextReader and store it in a list, but I 'm struggling to overcome this annoying little error. Let me explain a bit more:

I'm storing all the nodes from the file in an XmlNodeList, and using a foreach loop to read each node and add it to my list using this class template:

public class getGamesList
{
    public int id { get; set; }
    public string title { get; set; }        
    public string release { get; set; }        
    public string platform { get; set; }
}

Here is the main code:

List<getGamesList> list = new List<getGamesList>();
        String URLString = "http://thegamesdb.net/api/GetGamesList.php?name="+name;
        XmlTextReader tr = new XmlTextReader(URLString);

        XmlDocument xml = new XmlDocument();
        xml.Load(tr);
        XmlNodeList xnList = xml.SelectNodes("/Data/Game");

        foreach (XmlNode xn in xnList)
        {
            list.Add(new getGamesList()
            {
                id = Convert.ToInt32(xn["id"].InnerText),
                title = xn["GameTitle"].InnerText,
                release = xn["ReleaseDate"].InnerXml,
                platform = xn["Platform"].InnerText
            });
        }  

The code works fine up until the 3rd xml element, where there is a missing "ReleaseDate" node. It just doesn't have one. So an error is thrown.

I know I need to work out a way of checking if the node exists before I read it, but I'm getting in a bit of a mess; nothing so far has been successful. Does anyone have any ideas? Thanks.

Bagshot
  • 57
  • 1
  • 2
  • 7
  • 1
    People have given good answers already but for future reference always be specific about the error that you are getting. "An error is thrown" is much less useful than "A NullReferenceException is thrown in the marked line" or something like that. Exceptions come in different flavours precisely because they help you debug problems so always share them if you need help! ;-) – Chris Nov 29 '12 at 09:44
  • @Bagshot I have update my answer. maybe it would be useful for you. – KoViMa Nov 29 '12 at 10:02

3 Answers3

2

Try to check for null:

release = (null != xn["ReleaseDate"]) ? xn["ReleaseDate"].InnerXml : "",

EDIT:

Other way I would like to use is to add an XML attributes to your class:

[Serializable]
public class getGamesList
{
    [XmlElement("...")]
    public int id { get; set; }
    [XmlElement("...")]
    public string title { get; set; }        
    [XmlElement("ReleaseDate")]
    public string release { get; set; }        
    [XmlElement("...")]
    public string platform { get; set; }
}

This refactoring will allow you to use XmlSerializer.Deserialize (Example section) method and map XML to your class automatically.

KoViMa
  • 382
  • 1
  • 10
2

Check the object for null before accessing any properties.

release = xn["ReleaseDate"] != null ? xn["ReleaseDate"].InnerXml : string.Empty
Ravi Y
  • 4,296
  • 2
  • 27
  • 38
0

You could make an extesion method, something like

 public static T GetXmlValue<T>(this XmlNode node, string name, T defaultValue)
    {
        if (node != null && node[name] != null)
        {
            if (typeof(T) == typeof(string))
            {
                return (T)(object)node[name].InnerText;
            }
            else if (typeof(T) == typeof(int))
            {
                int value = 0;
                if (int.TryParse(node[name].InnerText, out value))
                {
                    return (T)(object)value;
                }
            }
        }
        return defaultValue;
    }

usage:

list.Add(new getGamesList()
{
    id = xn.GetXmlValue<int>("id", 0),
    title = xn.GetXmlValue<string>("GameTitle", string.Empty),
    release = xn.GetXmlValue<string>("ReleaseDate", string.Empty),
    platform = xn.GetXmlValue<string>("Platform", string.Empty)
});
sa_ddam213
  • 42,848
  • 7
  • 101
  • 110