' Input parameter file contains the path to the file to read.
' Output parameter doc is an XmlDoc variable that receives the
' parsed XML document that is generated.
Public Sub XmlReadFile(ByVal file As String, ByRef doc As XmlDoc)
Dim exc As Exception
doc = XmlDoc.LoadFile(file)
If (doc.ErrorCode <> 0) Then
Console.Writeline("Input error " & CStr(doc.ErrorCode) _
& ", " & doc.Message)
exc = New Exception
exc.ErrorCode = doc.ErrorCode
Throw exc
End If
End Sub
' Input parameter file contains the path to the file to write.
' Input parameter doc is an XmlDoc object that contains the
' document tree that is converted.
Public Sub XmlWriteFile(ByVal file As String, ByVal doc As XmlDoc)
Dim exc As Exception
Try
doc.SaveFile(file)
Catch exc
If doc.ErrorCode <> 0 Then
Console.Writeline("Output error " & _
CStr(doc.ErrorCode) & _
", " & doc.Message)
End If
Throw exc
End Try
End Sub
Assume we have an XML document tree that was generated from the following XML text:
<?xml version="1.0"?>
<procedureControl cellId="TestCell">
<command>start</command>
<procedure jobId="090507001" name="INS">
<type>protocol</type>
<testSet>INS_#_090507001</testSet>
<index>0</index>
</procedure>
</procedureControl>
The program below analyzes and displays part of the data contained in the tree.
Public Sub XmlParse(ByVal doc As XmlDoc)
Dim root As XmlNode
Dim command As XmlNode
Dim procedure As XmlNode
Dim attr As XmlNode
Dim ss As String
root = doc.DocumentElement
command = root.GetElementNode("command")
procedure = root.GetElementNode("procedure")
ss = root.GetAttribute("cellId")
Console.Writeline("cellId = " & ss)
ss = command.Value
Console.Writeline("command = " & ss)
ss = procedure.GetAttribute("jobId")
Console.Writeline("jobId = " & ss)
ss = procedure.GetElement("type")
Console.Writeline("type = " & ss)
ss = procedure.GetElement("testSet")
Console.Writeline("typeSet = " & ss)
End Sub
The output produced is:
cellId = TestCell
command = start
jobId = 090507001
type = protocol
typeSet = INS_#_090507001
If you do not know the structure of a document, you can search for an element by recursively searching through the tree until you find a match. This method is much slower than looking for an element where you expect to find it.
Public Sub XmlSearchTree(ByVal node As XmlNode, _
ByVal name As String, _
ByRef found As XmlNode)
Dim child As XmlNode
found = Nothing
If node.Name = name Then
If node.Type = "element" Then
Console.Writeline("Found " & name)
found = node
Return
End If
End If
child = node.FirstChild
While Not child Is Nothing
XmlSearchTree(child, name, found) 'recursive call
If Not found Is Nothing Then
Return
End If
child = child.NextSibling
End While
End Sub
The following program demonstrates how to create the XML document tree that corresponds to the XML text shown above in the third example. If this document is output using the XmlWriteFile program, the resulting XML text will be identical in content to the example except for indentation and line breaks.
Public Sub XmlCreate(ByRef doc As XmlDoc)
Dim root As XmlNode
Dim elem As XmlNode
doc = New XmlDoc("procedureControl")
root = doc.DocumentElement
root.AddAttribute("cellId", "TestCell")
root.AddElement("command", "start")
elem = root.AddElementNode("procedure")
elem.AddAttribute("jobId", "090507001")
elem.AddAttribute("name", "INS")
elem.AddElement("type", "protocol")
elem.AddElement("testSet", "INS_#_090507001")
elem.AddElement("index", "0")
End Sub