Agent Smith (aka Jason S) had me looking at what it would take to consume some XML files in VB6. Thankfully I had been doing work with VB recently and so had some clue's about what to do.

It should be noted that I was loading the XML into DOM, which actually puts the whole thing into memory so while it is flexible (forward and backwards reads) it will potentially consume a lot of memory.

'If we consider this XML snippet.


Agent
Smith
asmith@matrix.com


'Then we can access it like this.
Dim Xml As New MSXML2.FreeThreadedDOMDocument
Dim Root As MSXML2.IXMLDOMNode
Dim Agents As MSXML2.IXMLDOMNode
Dim Agent As MSXML2.IXMLDOMNode
Dim sLastName as String
Dim sFirstName as String
Dim sEmail as String
'Load the XML string to the DOM document
Xml.async = False
If Xml.Load(Left("C\MyFile.xml") Then
    Set Root = Xml.documentElement
    Set Agents = Root.selectSingleNode ("/MyInfo")
  sLastName = Agents.selectSingleNode("LastName").Text
  sFirstName = Agents.selectSingleNode("FirstName").Text
  sEmail = Agents.selectSingleNode("Email").Text
End If

'or if there are multiple agents as below looping could be done like this...



 
   Smith
   Agent
   asmith@matrix.com
 

 
   OtherGuy
   WithGlasses
   wg@matrix.com
 

 
   Last
   Agent
   la@matrix.com
 


Xml.async = False
If Xml.Load(Left("C:\MyFile.xml") Then
   Set Root = Xml.documentElement
    Set Agents = Root.selectSingleNode ("/MyInfo/MatrixAgents")
    For Each Agent In Agents.childNodes
  'Get Agent Smith
  if Agent.selectSingleNode("LastName").Text = "Smith" THen
   sLastName = Agent.selectSingleNode ("LastName").Text
   sFirstName = Agent.selectSingleNode("FirstName").Text
   sEmail = Agent.selectSingleNode("Email").Text
  End if
    Next
    GoTo Done
End If

'Remember to clean up after yourself with vb6 ... Xml = Nothing ... etc

"He who praises everybody, praises nobody." - Samuel Johnson

Technorati tags: , ,