How to get xmlItem.Value even if Element does not exist?
I am starting out with xml and vb.net here, my xml looks like this:
<?xml version="1.0" encoding="UTF-8"?>
<Steps>
<X cmd="Message">
<Message>Hello World</Message>
</X>
<X cmd="Message">
<Message>Hello World2</Message>
<Title>Cool Message</Title>
</X>
</Steps>
I have enumerated the whole xml and can access the items like this in one of my method:
Function showmsg(ByVal X As XmlElement)
Dim xTitle as String = X.Item("Title"开发者_开发百科).innerText
Dim xMessage as String = X.Item("Message").innerText
MsgBox(xMessage, , xTitle)
End Function
sooo, it works if all the elements are found (< Message > and < Title >).. but if for example the element "< Title >" is not found I get an error by trying to get the innerText.
Is there a way to ignore these errors, I just want to receive an empty value and not any errors. maybe there is a better way to do this?
You'll need to check if
X.Item("Title") Is Nothing
before retrieving its value
The other option is to us Try/Catch to ignore the errors, but that is going to slow you way down and get messy.
精彩评论