Link to home
Start Free TrialLog in
Avatar of BarCode99
BarCode99

asked on

Parsing a XMLDocument in a DLL - A.V.

Hi all,

I have a DLL that downloads a XML document from a website or a file share and puts it into a TMemoryStream. I then create a TXMLDocument and load the XML from the stream. Like this:

    XMLDocument := TXMLDocument.Create(nil);
    try
      XMLDocument.LoadFromStream(streamXMLFile);
      XMLDocument.Active := true;

Next I need to get one value out of the XML file and carry on doing other stuff, but here comes my problem... When I try to access XMLDocument.DocumentElement I get an Access Violation and I can't figure out why. My code continues like this:

      FNewApplicationVersion := XMLDocument.DocumentElement.ChildNodes.FindNode('ApplicationVersion').Text;   <------ Throws exception
    except
      on E: Exception do begin
        ShowMessage('Error: ' + E.Message);
      end;
    end;

The exception I get is "Access Violation at address 00002000. Read of address 00002000"

My variable declarations look like this:
var
  streamXMLFile : TMemoryStream;
  XMLDocument : TXMLDocument;
  FNewApplicationVersion : String;

As I mentioned this is a DLL. The same code works fine in a normal application... Any ideas?
I have done the trick with including ActiveX in my uses clause and calling CoInitialize in the initialization section.

Best regards,
Marius
Avatar of Wim ten Brink
Wim ten Brink
Flag of Netherlands image

XML is case-sensitive. Is it 'ApplicationVersion' or 'applicationversion'?
Avatar of BarCode99
BarCode99

ASKER

The text is copied straight from the XML file ("ApplicationVersion")...
Alternatively, are there other components I can use to parse the XML and read the value of a specific node easily?

I know about OpenXML. Does anybody have any experience with it?

Marius
OK I worked it out, but maybe someone can tell me what I did wrong???

If I change the declaration of XMLDocument from TXMLDocument to IXMLDocument (I not T) and remove the FreeAndNil it all works. The Create statement is still TXMLDocument.Create...

What about memory? Since there is no FreeAndNil, am I leaking like a sieve? Any thoughts?

Marius
no, when you use a local variable as interface type, it is automatically released when you no longer work with (at the end of procedure).
OK, so no worries then...

Thanks for you input  guys.

I'm going to ask for my points back as I solved it myself...

Marius
PAQ and refund acceptable by me.
It's ok for me too.
ASKER CERTIFIED SOLUTION
Avatar of modulo
modulo

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial