How to Determine if element contains a given attribute
-
Code along the lines of:
string value1 = currentNode.Attributes["value1"].Value;
However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike Stanbrook -
Code along the lines of:
string value1 = currentNode.Attributes["value1"].Value;
However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike StanbrookIF NOT currentNode.Attributes.ItemOf("value1") IS NOTHING THEN value1 = currentNode.Attributes.Item("value1") END IF
-
Code along the lines of:
string value1 = currentNode.Attributes["value1"].Value;
However, if the Node does not contain an attribute named "value1", and exception would be thrown. So, is there a way (without kludging with try..catch blocks) to determine if a given attribute exists for a node? TIA ~Mike StanbrookThere is an easier way by using XPath Queries.
...asuming you have an XMLDocument Open
Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
If (Not IsNothing(xNode)) Then
'Do Something
End If -
There is an easier way by using XPath Queries.
...asuming you have an XMLDocument Open
Dim xNode As Xml.XmlNode = xDoc.SelectSingleNode("//Element[@Attribute='Value']")
If (Not IsNothing(xNode)) Then
'Do Something
End IfAre you sure this is easier/more efficient that the method previously suggested, especially given that the current element node is already known and sotred in a local variable? ~Mike Stanbrook