Reputation: 1795
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);
}
}
Upvotes: 0
Views: 59
Reputation: 18411
Checking .Value
or any other property on a null
object is illegal:
if (e.Attribute("att3") != null)
Upvotes: 1