TX Text Control ActiveX

Editing XML Files

Topic

Editing an XML file in TXTextControl differs from editing normal text in a few ways: selections stay inside one element, the TAB key moves from element to element and formatting is changed through styles only. After a valid XML document is loaded, TXTextControl switches to its XML editor mode, which keeps the document valid according to its DTD, the document type definition that holds the rules of the document's structure.

The sections below use an address list as the example and end with the Visual Basic sample program that inserts and deletes elements from the button bar.

The Example Document

XML documents consist of elements. Some elements contain the document's text, others represent the document's structure. The following XML document contains a list of addresses:

<?xml version="1.0"?>
<?XML:stylesheet type="text/css" href="addresses.css"?>
<!DOCTYPE Addresses SYSTEM "addresses.dtd">
<Addresses>
   <DocumentHeadline>Addresses</DocumentHeadline>
   <AddressTable>
      <Address>
         <Name>Jack Miller</Name>
         <Street>Baker Street</Street>
         <City>New York</City>
      </Address>
      <Address>
         <Name>Tom Baker</Name>
         <Street>Miller Street</Street>
         <City>Los Angeles</City>
      </Address>
      <Address>
         <Name>Bill Taylor</Name>
         <Street>Old Street</Street>
         <City>Washington</City>
      </Address>
   </AddressTable>
</Addresses>

The elements DocumentHeadline, Name, Street and City contain the document's text. The elements Addresses, AddressTable and Address are structural elements without any text. Programming XML Documents uses the same document and shows its DTD, addresses.dtd.

How the XML Editor Mode Works

After the document has been loaded, TXTextControl displays it WYSIWYG with the formatting styles of the CSS file that the document names, and switches to the XML editor mode automatically. The XMLEditMode property tells whether this mode is active. In this mode:

  • Text is inserted and deleted as in any other document.
  • Selections are possible only inside a single XML element. When a user tries to select more than one element, TXTextControl expands the selection to its maximum, the complete text of the single element.
  • The TAB key selects the complete text of the next element, and SHIFT+TAB the text of the previous element, as in tables.
  • Clipboard operations work as in any other document. Text inserted from another document is inserted without any formatting attributes.

Format an XML Document

XML documents contain no formatting attributes, so their formatting can be changed only through formatting styles:

  • The button bar changes the attributes of the current style when the user formats text with it.
  • Formatting functions are disabled in XML documents.
  • The built-in style dialog boxes, which the StyleDialog method opens, and the style collection, which the Styles property returns, change the formatting attributes.

To make these changes permanent, save the new attributes explicitly in the CSS file. Loading and Saving XML Files shows how.

Try the XML Editing Sample

The Visual Basic sample program in the Samples\vb6\XMLEditing sample source directory shows these differences in practice. Run it and load the demo file address_list.xml. The button bar now shows three combo boxes:

The Editing XML Files sample program with address_list.xml loaded: three combo boxes in the button bar, the first showing name, above the address records of Paul, George and Ringo.

You don't create the combo boxes yourself: they are built into the button bar, TXButtonBar.

Show the Current Element

The first combo box displays the style of the current XML field. Move the caret down with the DOWN ARROW key, and the display changes from name to street, city and email, depending on the type of field the caret is in. The PosStyle property sets the position of the formatting styles combo box.

Insert Elements

The second combo box inserts additional XML fields. Its position is set with the PosXMLAdd property.

The second combo box of the button bar opened, listing the elements that can be inserted: address_record, street and city.

You can insert either a complete address record or one of the optional elements, street and city. Depending on the DTD, not all elements can be inserted at all positions. For example, click the name Paul in the first line of the document and try to insert a street: an error occurs, because this record already contains a street. Try again with Ringo, and the street is inserted.

Delete Elements

The third combo box deletes XML fields, and its position is set with the PosXMLRemove property. Every action is checked against the DTD, so deleting a required field, for example, can't make your document invalid.

Next Steps