Loading and Saving XML Files
TXTextControl has built-in features for editing and manipulating XML files, and it loads and saves them like any other file, with the Load and Save methods and the XML format (10). What happens during loading depends on the document: whether it is well-formed, whether it matches its document type definition (DTD) and whether a CSS file with formatting styles belongs to it.
Read on for the three results a load can have, the events that report errors and what a save writes to the XML file and to the CSS file.
How an XML File Is Loaded
When TXTextControl loads an XML file, it reads the DTD that belongs to the document and validates the document against it. A DTD, the document type definition, holds the rules for the elements an XML document may contain and how they are ordered and nested. The result decides how the document can be edited:
| The document is | Event | What TXTextControl does |
|---|---|---|
| Valid according to its DTD | None | Loads the text and the formatting styles and switches to the XML editor mode |
| Invalid: it doesn't follow the rules of the DTD | XMLInvalid | Loads the text and the formatting styles; the document can be edited as a normal text document with all editing features |
| Not well-formed: it contains invalid XML syntax | XMLNotWellFormed | Doesn't load the document |
- The XML editor mode keeps the document valid while it is edited. Editing XML Files describes how it works, and the XMLEditMode property tells whether
TXTextControlis in this mode. - An invalid document is loaded, but the reference of the
XMLInvalidevent adds that it cannot be handled or saved as an XML document. - Both events pass the position in the file where the error occurred and the reason for the error to your application.
Formatting Styles From the CSS File
An XML file holds no formatting. During loading, TXTextControl looks for the formatting styles in the CSS file that belongs to the document. If it finds the file, it loads the styles and displays the document with all its formatting attributes, and all further editing and manipulation of the document takes place in WYSIWYG mode. XML Basics shows an XML file, its CSS file and its DTD side by side.
Insert an XML Document Into Another Document
An XML document that is inserted into another, existing document becomes invalid. For this reason, TXTextControl doesn't load an XML document when the CurSelection parameter of the Load method is set to True, which would insert the file at the current input position.
Save an XML Document
The Save method saves only the text of an XML document into the XML file. To save the formatting styles of the document into its CSS file, call Save a second time with the CSS format (11).
TXTextControl has the following limitations:
- Changing and saving the DTD of a document isn't supported.
- Creating a completely new XML document isn't supported.
Next Steps
- Editing XML Files: the XML editor mode, selections and formatting styles.
- Programming XML Documents: reading, changing, inserting and deleting elements in code.
- XML Basics: a sample program with an XML file, a CSS file and a DTD.