0

When I create a TXMLDoucument oject with the constructor with a file path, it is not parsed and I got "Invalid pointer operation" when running the following code.

void __fastcall TForm1::Button1Click(TObject *Sender)
    {
     String filePath="C:\\Users\\Maksim\\Documents\\SteelList.xml";       
     TXMLDocument* xmlDoc=new TXMLDocument(filePath);        
     xmlDoc->DOMVendor=GetDOMVendor("MSXML");        
     xmlDoc->Active=true;        
     String nodeName=xmlDoc->DocumentElement->GetNodeName();       
     ShowMessage(nodeName);      
    }

However when I create a TXMLDocument oject with the constructor with an Owner parameter, the following code works fine.

void __fastcall TForm1::Button1Click(TObject *Sender)
{
 String filePath="C:\\Users\\Maksim\\Documents\\SteelList.xml";    
 TXMLDocument* xmlDoc=new TXMLDocument(this);    
 xmlDoc->FileName=filePath;    
 xmlDoc->DOMVendor=GetDOMVendor("MSXML");    
 xmlDoc->Active=true;    
 String nodeName=xmlDoc->DocumentElement->GetNodeName();    
 ShowMessage(nodeName);    
} 
  • 1
    You need to use IXMLDocument instead when creating without an owner. See [this Delphi post](https://stackoverflow.com/q/8398703/62576). – Ken White Jun 09 '19 at 01:12
  • @KenWhite So,I cannot use my object because it is freed after all references to its interface are released. But why is it so? I guess I have a refrence here `String nodeName=xmlDoc->DocumentElement->GetNodeName()` – maksim_volodin Jun 09 '19 at 05:53
  • No, a `String` is not a reference to an interface. It's a `String`. If you need the XML document outside the button click handler, then declare it in the form itself instead as a private member. – Ken White Jun 09 '19 at 23:21

0 Answers0