Technical Articles > Working with XML Files
Text Control exposes the elements of an XML document through collections of XMLElement objects. The "XMLElements property returns an XML collection. There is one collection for each kind of elements. In the example document above there is a collection for all Address elements and another collection for all Name elements. The following line of code gets the collection of the Name elements:
Set NameElements = TXTextControl1.XMLElements("Name")
A collection object has the standard Item method to access a single element. With the XMLElement object's Text method the following line of code displays the content of the second Nameelement:
MsgBox TXTextControl1.XMLElements("Name").Item(2).Text
The Item method is the default method of the XMLElements collection thus the following line of code is equivalent:
MsgBox TXTextControl1.XMLElements("Name")(2)
The Text property of elements containing sub-elements is the complete text of all sub-elements separated through Chr(10) characters. For example the line of code:
TXTextControl1.XMLElements("Address")(2) = "Paul Baskerville" & Chr(10) & "Jackson Street" & Chr(10) & "London"
in the example above displays:
Tom Baker Miller Street Los Angeles
To set a new text with the Text property specify a text string separated through Chr(10) characters:
TXTextControl1.XMLElements("Address")(2) = "Paul Baskerville" & Chr(10) & "Jackson Street" & Chr(10) & "London"
With a For Each statement a program can iterate through an XML collection. The following example displays all names:
For Each element in TXTextControl1.XMLElements("Name") MsgBox element.Text Next
For development enviroments that do not support For Each processing, the above code is equivalent to the following code using the collection's Count property:
Set elements = TXTextControl1.XMLElements("Name") For i = 1 To elements.Count MsgBox elements(i).Text Next i
A Text Control XML collection also has an Add and a Remove method to insert or delete elements. This is explained in the following section.
<< Editing XML Files | >> Inserting and Deleting XML Elements