Properties of a null value

So, I’ve been doing some work with Linq to XML, and I have found myself writing the following code pattern very often:

#Note: $element is of type [System.Xml.Linq.XElement] $attribute_value = $null $attribute = $element.Attribute("AttributeName") if($attribute -ne $null) { $attribute_value = $attribute.Value } Write-Host "The value of the attribute is $attribute_value" 

I have tested, and this code appears to produce the same result:

#Note: $element is of type [System.Xml.Linq.XElement] $attribute_value = $element.Attribute("AttributeName").Value Write-Host "The value of the attribute is $attribute_value" 

This seems to rely on a default behavior of Powershell that if I get a property of a null value, that property is returned as $null as well.

Is this the right way to go about things? Or… am I doing it wrong?

submitted by /u/binarycow
[link] [comments]

Leave a Reply