0
    public string GetLogName(string config)
    {
        XDocument xDoc = XDocument.Load(config);
        XElement[] elements = xDoc.Descendants("listeners").Descendants("add").ToArray();

        foreach (var element in elements)
        {
            if (element.Attribute("fileName").Value != null)
            {
                string filename = element.Attribute("fileName").Value;
                int location = filename.IndexOf("%");
                Console.WriteLine("string to return: " + filename.Substring(0, location));
                return filename.Substring(0, location);
            }
        }
    }

I am trying to retrieve the "fileName" attribute from each element in the elements array, but there are some cases when the "fileName" attribute does not exist and fails with the following error: NullReferenceException was unhandled. Object reference not set to an instance of an object.

In my case there are two "add" nodes that do not have a "fileName" attribute, but the third add node has it.

How can I skip over entries that do not have a "fileName" attribute, or can you recommend a better way to retrieve this attribute?

John Saunders
  • 157,405
  • 24
  • 229
  • 388
Pam
  • 63
  • 5
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Oct 17 '13 at 17:50

3 Answers3

0

You should be able to do this simply by changing this line:

if (element.Attribute("fileName").Value != null)

To:

if (element.Attribute("fileName") != null)
cdhowie
  • 133,716
  • 21
  • 261
  • 264
0

change your if statement to this :

if (element.Attribute("fileName") != null)
Jonesopolis
  • 23,589
  • 10
  • 63
  • 106
0

One way is to filter out the list before you process it:

XElement[] elements = xDoc.Descendants("listeners")
                          .Descendants("add")
                          .Where (d => d.Attribute("filename") != null )
                          .ToArray();

--- IMHO this is how I would rewrite the method, using linq and regex ---

var elements =
XDocument.Load(config);
         .Descendants("listeners")
         .Descendants("add")
         .Where (node => node.Attribute("filename") != null )
         .ToList();


return elements.Any() ? elements.Select (node => node.Attribute("filename").Value )
                                .Select (attrValue => Regex.Match(attrValue, "([^%]+)").Groups[1].Value)
                                .First ()
                      : string.Empty;
ΩmegaMan
  • 22,885
  • 8
  • 76
  • 94
  • @Pam Thanks...see suggestions on rewrite. Note the .Select extension which is a project extension to change data from one form to another...quite powerful. GL – ΩmegaMan Oct 17 '13 at 18:24
  • I decided to use this approach to filter out the other nodes instead of doing the check afterward. I did not know how to write the where clause for this query so this really helped. Thanks! – Pam Oct 17 '13 at 18:27