How to get the root element?

Thierry

Member
Joined
May 15, 2008
Messages
7
Programming Experience
3-5
Hi,

I'm trying to get the root element name with VB.NET (2005)
For example:
HTML:
<?xml version="1.0"?>
<labels>
	<category name="Videos">
		<utterance>12 Kings</utterance>
		<utterance>Home Alone 1</utterance>
		<utterance>Elvis Presley, Viva las Vegas</utterance>
	</category>
	<category name="Books">
		<utterance>Lord of the Rings</utterance>
		<utterance>Itt</utterance>
	</category>
</labels>
How to get the Root Element name (in the example "Videos" or "Books") if I only have the utterance attribute like "Home Alone 1" or "Itt"?

Thanks in Advance,

Thierry
 
XPath is a language for finding information in an XML document.
VB.NET:
Dim nodes As Xml.XmlNodeList = doc.SelectNodes("//category[utterance='Home Alone 1']/@name")
'doc' is here the XmlDocument. To find text "Videos" in this case you check if there are any nodes found (nodes.Count>0), then the Value of each node is here the attribute text.
 
Actually you would probably only want a single node here, so using SelectSingleNode method (same query path) will return the first match if any.
 
VB.NET:
        Dim xpathDoc As XPathDocument
        Dim xmlNav As XPathNavigator
        Dim xmlNI As XPathItem

        xpathDoc = New XPathDocument(Filepath)
        xmlNav = xpathDoc.CreateNavigator()
        xmlNI = xmlNav.SelectSingleNode("//category[utterance='" + LBUtterance.SelectedItem.ToString + "']/@name")

        [COLOR="red"]LblCategory.Text = xmlNI.ToString[/COLOR]

Alright, I got the following problem now. I can't show my selected text in a label.

Sorry for the noobie questions, has been ages since I programmed in VB.NET :rolleyes:
 
If there is a chance a query will not return any matches you have to check the result before using it. For node lists you check Count as mentioned, for single nodes you check agaist Nothing. For your XPathNavigator.SelectSingleNode usage you have to check if xmlNI is Nothing. The problem exact with "xmlNI.ToString" is that xmlNI is Nothing because nothing was found and you can't call ToString method on Nothing.
 
VB.NET:
        ' Case 1:
        Dim xpathDoc As XPathDocument
        Dim xmlNav As XPathNavigator
        Dim xmlNI As XPathItem

        xpathDoc = New XPathDocument(Filepath)
        xmlNav = xpathDoc.CreateNavigator()
        xmlNI = xmlNav.SelectSingleNode("//category[utterance='" + LBUtterance.SelectedItem.ToString + "']/@name")

        If xmlNI.Value <> "" Then
            LblCategory.Text = xmlNI.Value.ToString
        End If

It works! :D
 
No, you are doing the same error as previous post. If a node is not found xmlNI will be Nothing and you can't access Value property on a null reference. If the SelectSingleNode query could result in no matches you must check if xmlNI is Nothing before you can use it.
 
Thanks, I understand what you mean but in my situation that's impossible. The names always exist because they are selected from a listbox.

Edit: Thanks allot JohnH. You actually helped me allot (forgot to thank you for your time) ^^
 
Last edited:
Back
Top