How To Select a Single Node in an XML Document from a TreeView in Visual Basic

This article was previously published under Q252514
This article has been archived. It is offered "as is" and will no longer be updated.
One of the methods added to the IDOMNode object in the IXMLDOMNode interface in Microsoft Internet Explorer 5 (IE5) is the SelectSingleNode method. This method applies a specified pattern-matching operation to a node's context and returns the first matching node. This behavior is similar to the SelectNode method, which returns a list of matching nodes.
The following sample demonstrates one implementation of this method. This sample opens an ADO Recordset and saves the data to an XML Document file using the FileSystemObject. This XML file is subsequently opened, and a Visual Basic TreeView control is filled with its data. Subsequently, the SelectSingleNode method is implemented to select a single node in the XML Document based on an item chosen from the TreeView control.

If a newer version of MSXML has been installed in side-by-side mode, you must explicitly use the Globally Unique Identifiers (GUIDs) or ProgIDs for that version to run the sample code. For example, MSXML version 4.0 can only be installed in side-by-side mode. For additional information about the code changes that are required to run the sample code with the MSXML 4.0 parser, click the following article number to view the article in the Microsoft Knowledge Base:
305019 INFO: MSXML 4.0 Specific GUIDs and ProgIds
NOTE: The $ieq$ syntax is used here for pattern matching against a Node Element. However, the "@" syntax can be used for Attribute pattern matching.
  1. To run this sample, start a new Visual Basic Standard Exe project. Form1 is created by default.
  2. On the Project menu, under References, select the following items: Microsoft ActiveX Data Objects Library
    Microsoft XML, Version 1.0 or later
    Microsoft Scripting Runtime*
    *The FileSystemObject Object Model is contained in the Microsoft Scripting Runtime Library.

  3. From the Project menu, add a standard Module and copy the following code to create the appropriate variables needed in the project:
    Option ExplicitPublic cn As New adodb.ConnectionPublic rs As New adodb.RecordsetPublic myI As IntegerPublic StrTmp As StringPublic Errs1 As ErrorsPublic myFSO As New FileSystemObjectPublic myTxtStream As TextStreamPublic myfld As adodb.FieldPublic domdoc As New MSXML.DOMDocumentTo specify a specific version, use a declaration like the following, with the appropriate version in the ProgID:Public domdoc As New MSXML2.DOMDocument40					
  4. Draw the following controls on Form1 and set their Properties as listed below:
    Control		        Name			CaptionCommand Button	        cmdOpenRS               Open ADO RecordsetCommand Button	        cmdSaveRSasXML          Save RS as XML using FSOCommand Button	        cmdPopulateTview        Populate TreeViewCommand Button	        cmdExit                 ExitLabel		        lblNodeSelected         Node Selected:Text BoX                txtNodeSelectedTreeView Control*       xtvFSO*The TreeView Control is part of the Windows Common Controls.Therefore, you'll want to check this off in your Project-Components list.					
  5. Copy the following code into the General Declarations section of the code window:NOTE: Replace your Server Name, User ID, and Password information in the Connection String.
    Option ExplicitPrivate Sub cmdOpenRS_Click()On Error GoTo AdoErrorScreen.MousePointer = vbHourglass'Check to be sure the connection is not already opened:If cn.State = 1 Then    cn.Close    Set cn = NothingEnd Ifcn.Open "driver={SQL Server};Server=YourServer; " & _"uid=YourUID;pwd=YourPWD;database=Northwind"'Check to be sure a Recordset is not already opened:If rs.State = 1 Then    rs.Close    Set rs = NothingEnd IfWith rs    .CursorLocation = adUseClient    .CursorType = adOpenStatic    .LockType = adLockOptimistic    .CacheSize = 50    .Source = "Select CustomerID, " & _    "CompanyName, ContactName, Region from Customers"    .ActiveConnection = cn    .OpenEnd Withrs.MoveLastrs.MoveFirst'txtFsoRecordCount.Text = rs.RecordCountMsgBox "There are " & rs.RecordCount & " Records in RS"Screen.MousePointer = vbNormalExit SubAdoError:Dim errLoop As ErrorDim strError As StringmyI = 1StrTmp = StrTmp & vbCrLf & "VB Error # " & Str(Err.Number)StrTmp = StrTmp & vbCrLf & "   Generated by " & Err.SourceStrTmp = StrTmp & vbCrLf & "   Description  " & Err.Description' Display Error Information for each Error object.Set Errs1 = cn.ErrorsFor Each errLoop In Errs1    With errLoop        StrTmp = StrTmp & vbCrLf & "Error #" & myI & ":"        StrTmp = StrTmp & vbCrLf & "   ADO Error   #" & .Number        StrTmp = StrTmp & vbCrLf & "   Description  " & .Description        StrTmp = StrTmp & vbCrLf & "   Source       " & .Source        myI = myI + 1    End WithNextMsgBox StrTmpEnd SubPrivate Sub cmdExit_Click()If cn.State = 1 Then    cn.Close    Set cn = NothingEnd IfIf rs.State = 1 Then    rs.Close    Set rs = NothingEnd IfUnload MeEnd SubPrivate Sub cmdSaveRSasXML_Click()Screen.MousePointer = vbHourglass'If the file already exists, first Kill it.'then create it:'If it does not already exist, simply create it:On Error GoTo mdkNoSeeEmConst mdkNoSeeEm = 53 'File Not FoundKill "Customers.xml"'Create a Text file using the FileSystemObject:Set myTxtStream = myFSO.CreateTextFile(App.Path & "\Customers.xml", True, True)'Write data to the Text File to compile'the XML Document using the TextStream:myTxtStream.WriteLine "<?xml version=""1.0"" standalone=""yes""?>"myTxtStream.WriteLine "<Customers>"rs.MoveFirst    Do While Not rs.EOFmyTxtStream.WriteLine "<Customer>"myTxtStream.WriteLine "<CustomerID>" & ReplaceEntXMLSpecChar(rs.Fields("CustomerID") & "", "&", "amp;") & "</CustomerID>"myTxtStream.WriteLine "<CompanyName>" & ReplaceEntXMLSpecChar(rs.Fields("CompanyName") & "", "&", "amp;") & "</CompanyName>"myTxtStream.WriteLine "<ContactName>" & ReplaceEntXMLSpecChar(rs.Fields("ContactName") & "", "&", "amp;") & "</ContactName>"myTxtStream.WriteLine "<Region>" & ReplaceEntXMLSpecChar(rs.Fields("Region") & "", "&", "amp;") & "</Region>"myTxtStream.WriteLine "</Customer>"rs.MoveNextLooprs.MoveFirstmyTxtStream.WriteLine "</Customers>"myTxtStream.CloseScreen.MousePointer = vbNormalExit SubmdkNoSeeEm:    If Err.Number = mdkNoSeeEm Then        Resume Next    End If    Screen.MousePointer = vbNormalEnd SubPrivate Function ReplaceEntXMLSpecChar(ByVal strSource As String, ByVal strSearchFor As String, ByVal strReplace As String) As String    Dim lngPointer As Long, strNew As String    lngPointer = InStr(strSource, strSearchFor)    If lngPointer = 0 Then        ReplaceEntXMLSpecChar = strSource    Else        strNew = Left$(strSource, lngPointer - 1) & strReplace & Mid(strSource, lngPointer + 1, Len(strSource))        ReplaceEntXMLSpecChar = strNew    End IfEnd FunctionPrivate Sub cmdPopulateTview_Click()On Error GoTo MyXMLPerr'Fill The TreeView with data from the XML Document:With domdoc    .async = False    .Load App.Path & "\customers.xml"End WithIf domdoc.readyState = 4 Then    xtvFSO.Nodes.Clear    AddNode domdoc.documentElementEnd IfExit SubMyXMLPerr:    MsgBox domdoc.parseError.reason & vbCrLf & " Line Number In Document: " & domdoc.parseError.Line & vbCrLf & domdoc.parseError.srcTextEnd SubPrivate Sub xtvFSO_DblClick()Dim mytempTVnode As StringDim SelectedNode As IXMLDOMNodemytempTVnode = xtvFSO.SelectedItemSet SelectedNode = domdoc.documentElement.SelectSingleNode("//Customers/Customer[CustomerID $ieq$ '" & mytempTVnode & "']")txtNodeSelected.Text = SelectedNode.childNodes(2).TextEnd Sub<BR/><BR/>Private Sub AddNode(ByRef myNode As IXMLDOMNode, Optional ByRef TreeNode As Node)Dim tvcusterrNode As NodeDim tvcusterrNodeList As IXMLDOMNodeListDim i As Long                 If TreeNode Is Nothing Then        Set tvcusterrNode = xtvFSO.Nodes.Add    Else        Set tvcusterrNode = xtvFSO.Nodes.Add(TreeNode, tvwChild)    End If           tvcusterrNode.Expanded = True    tvcusterrNode.Text = myNode.nodeName           If tvcusterrNode.Text = "#text" Then           tvcusterrNode.Text = myNode.nodeTypedValue    Else           tvcusterrNode.Text = "<" & tvcusterrNode.Text & ">"    End If    Set tvcusterrNodeList = myNode.childNodes    For i = 0 To tvcusterrNodeList.length - 1        AddNode tvcusterrNodeList.Item(i), tvcusterrNode    NextEnd Sub					
  6. Run the project.
  7. Click the Open ADO Recordset Commandbutton to open a Connection and Recordset, displaying the RecordCount Property.
  8. Click the Save RS as XML Commandbutton to create the XML Document file.
  9. Click the Populate TreeView Commandbutton to fill the TreeView control.
  10. Double-click on any Customer ID in the TreeView, and note that the Contact Name for that Customer ID is displayed in the Text Box.

Article ID: 252514 - Last Review: 02/28/2014 00:30:42 - Revision: 1.2

  • Microsoft XML Parser 2.0
  • Microsoft XML Parser 3.0
  • Microsoft XML Core Services 4.0
  • Microsoft Visual Basic Enterprise Edition for Windows 6.0
  • Microsoft ActiveX Data Objects 2.1
  • Microsoft ActiveX Data Objects 2.5
  • Microsoft ActiveX Data Objects 2.6
  • kbnosurvey kbarchive kbhowto KB252514