Hey everyone,

I've looked at XML and VB/XML tutorials 'till they're coming out my ears and I'm still quite confused. So I thought I'd tackle a trivial task to see if my understanding is OK.

I thought I'd create an XML database of People, where each Person therein has a LastName and a FirstName, thusly:

Code:
<People>
   <Person>
      <LastName>Fred</LastName>
      <FirstName>Bloggs</FirstName>
  </Person>
.... more persons
</People>
The task was to load the XML file first; that's easy and I hardcoded the data into the VB.

Then put a LastName in a textbox, and search the XML for that name. For each one found, put the corresponding FirstName in a listbox.

Criteria:

  • By mistake, there may be elements other than <Person> right off the root; handle that
  • The <FirstName>, <LastName> elements in a <Person> may be missing or in the wrong order; handle that
  • There will likely be more than one 'FirstName' for any 'LastName; hence the listbox


My confusion came in looping thru' the nodes. Please someone check the following code to see if I've gone about this the long way round....

For each node off the root, I check it's a Person; if not it's a spurious entry.

Then in each Person, I check if there's a LastName whose text is the req'd surname from the textbox. If it is, then I check LastName's siblings to see if there's a FirstName, and if there is I bung it in the listbox.

What worries me is that this seems a very long way of doing a SELECT..... FROM..... WHERE sort of thing?

VB Code:
  1. 'just the part that loops thru' the nodes as described above..
  2.  
  3. For Each Child In xmlRoot.childNodes
  4.   If Child.nodeName = "Person" Then
  5.      For Each Child2 In Child.childNodes
  6.          If Child2.nodeName = "LastName" And Child2.Text = TextLastName Then
  7.              For Each Child3 In Child.childNodes
  8.                 If Child3.nodeName = "FirstName" Then
  9.                  ListFirstName.AddItem Child3.Text
  10.                 End If
  11.              Next Child3
  12.          End If
  13.      Next Child2
  14.   Else
  15.   MsgBox "Node name: " & Child.nodeName, , "Non person!"
  16.   End If
  17. Next Child

So any thoughts anyone?