0

I have an list of "IEnumerable " all of the same elements: "elem1", that I got by

IEnumerable <XElement> childList = 
                    from el in sessionXML.DescendantsAndSelf().Elements("elem1")
                    select el;

childList:

<elem1 att1= "..." att2= "..."> </elem1>
<elem1 att1= "..." att2= "..." att3 = "..."> </elem1>
<elem1 att1> </elem1>

Not all of the elements have the same attributes. I'm trying to check the existence of att3, if so, I'd like to print this element, when I did below code, it still gives me a "Object reference not set to an instance of an object" error:

 foreach (XElement e in childList)
        {
            //Check if attribute "target" exists
            if (e.Attribute("att3").Value != null)
            {
                Console.writeLine(e);
            }
        }
jerryh91
  • 1,777
  • 10
  • 46
  • 77

1 Answers1

1

Checking .Value or any other property on a null object is illegal:

if (e.Attribute("att3") != null)
Giannis Paraskevopoulos
  • 18,261
  • 1
  • 49
  • 69