VB 6 - Microsoft XML 3.0

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.

<?xml version='1.0'?>
	<Email><a href="mailto:asmith@matrix.com">asmith@matrix.com</a> </Email>

Then we can access it like this...

Dim Xml As New MSXML2.FreeThreadedDOMDocument
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...

<?xml version='1.0'?>
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
    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: , ,

Comment Section

Comments are closed.