I use TXMLDocument to create XML documents. Sometimes I need to change attribute values. I get a “Namespace error” if I use the “ADOM XML v4” DOM vendor (Delphi XE2).
Example code:
procedure TForm1.Button1Click(Sender: TObject);
var
XML: TXMLDocument;
XMLNode, XMLSubNode: IXMLNode;
begin
XML := TXMLDocument.Create(nil);
//XML.DOMVendor := GetDOMVendor('MSXML'); // Works using MSXML
XML.DOMVendor := GetDOMVendor('ADOM XML v4');
XML.Active := True;
XMLNode := XML.AddChild('test');
XMLNode.Attributes['state'] := 1;
XMLNode.Attributes['state'] := 0; // Raises "Namespace error"
end;
If I use MSXML, then everything works fine. I want to use ADOM XML because I’m generating large XML files and it seems to be a lot faster than MSXML.
How do I change the attribute value?
This is a bug. A pretty major one too. With the current implementation of the ADOM XML vendor, if you create an attribute in the null namespace, you cannot change its value.
Here is the offending code from the AdomCore_4_3 unit, as bundled with Delphi 2010.
In the above, the issue can be isolated to here…
Work-around 1
I have no idea what the author’s intent was, but as it stands, this test is nonsense. To fix, remove this test and recompile.
Work-around 2
As an alternative, you could delete the attribute before setting like this …