# Editing XML Files

> Edit XML documents in the XML editor mode of TXTextControl: element selections, the TAB key, style-based formatting and the button bar XML combo boxes.

- **Product:** TX Text Control ActiveX
- **Section:** Documentation
- **Kind:** Topic
- **Path:** TX Text Control ActiveX > Document Editor > Working With XML Files > Editing XML Files
- **1688 words**
- **Web URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/
- **LLMs URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms.txt
- **LLMs-Full URL:** https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/editing/llms-full.txt

---

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
<?xml version="1.0"?>
<?XML:stylesheet type="text/css" href="addresses.css"?>

<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](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/xmleditmode-property/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styledialog-method/llms-full.txt) method opens, and **the style collection**, which the [Styles](https://docs.textcontrol.com/tx-text-control-activex/reference/txtextcontrol/styles-property/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/load-save/#save-an-xml-document) 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.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/xml-editing-sample.fdc2636e.webp)

You don't create the combo boxes yourself: they are built into the button bar, [TXButtonBar](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/llms-full.txt).

#### 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](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/posstyle-property/llms-full.txt) 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](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/posxmladd-property/llms-full.txt) property.

![The second combo box of the button bar opened, listing the elements that can be inserted: address_record, street and city.](https://docs.textcontrol.com/assets/img/tx-text-control-activex/xml-insert-element-list.f55954ea.webp)

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](https://docs.textcontrol.com/tx-text-control-activex/reference/txbuttonbar/posxmlremove-property/llms-full.txt) property. Every action is checked against the DTD, so deleting a required field, for example, can't make your document invalid.

### Next Steps

- [Programming XML Documents](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/llms-full.txt): reading, changing, inserting and deleting elements in code.
- [Loading and Saving XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/load-save/llms-full.txt): validation, load errors and saving the CSS file.
- [XML Basics](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/basics/llms-full.txt): how an XML file, its CSS file and its DTD work together.

---

## Nearby

- **Previous:** [Loading and Saving XML Files](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/load-save/llms-full.txt)
- **Next:** [Programming XML Documents](https://docs.textcontrol.com/tx-text-control-activex/document-editor/xml/programming/llms-full.txt)
