Salvador
Salvador

Reputation: 16472

how i can add the version and encoding to a xml file using TXMLDocument

i want to add the version and encodig to a xml file created with TXMLDocument component

<?xml version="1.0" encoding="utf-8"?>

curently i'm doing this

XmlDoc   :=TXMLDocument.Create(nil);
XmlDoc.Version:='1.0';
XMLDoc.Encoding:='utf-8';

but i receive an acces violation in this line

XmlDoc.Version:='1.0';

how i can add the version and encoding?

Upvotes: 6

Views: 8617

Answers (2)

Remy Lebeau
Remy Lebeau

Reputation: 595349

If you construct a TXMLDocument with a nil Owner, the new instance uses reference counting to maintain its lifetime, so you MUST assign it to an IXMLDocument variable to maintain the reference count correctly or else the instance will be freed prematurely. Do not use a TXMLDocument variable in that situation. This is documented behavior, and would account for your AV. When working with dynamic instances of TXMLDocument, it is better to use the NewXMLDocument() and LoadXML...() functions instead.

Upvotes: 1

RRUZ
RRUZ

Reputation: 136391

you must set the Active property to True before to modify the XML document properties.

XmlDoc   :=TXMLDocument.Create(nil);
XmlDoc.Active:=True;
XmlDoc.Version:='1.0';
XMLDoc.Encoding:='utf-8';

Upvotes: 18

Related Questions