这样完全没问题,但是为了方便循环调用,我要把它放到函数中去procedure TForm1.Button1Click(Sender: TObject);
var
Root,n1,n2,data:IXMLNode;
xdoc:TXMLDocument;
begin
xdoc := TXMLDocument.Create(nil);
xdoc.LoadFromXML(xmlsource);
  xdoc.Active := true;
  Root := xdoc.DocumentElement;
  n1:=Root.ChildNodes[0];
  n2:=n1.ChildNodes.First;
  showmessage(n2.text);
end;放函数中,就会出错,求解决办法
错误提示为:Invalid pointer operation
function xmltohtml(xmlsource:string):string;
var
Root,n1,n2,data:IXMLNode;
xdoc:TXMLDocument;
begin
xdoc := TXMLDocument.Create(nil);
xdoc.LoadFromXML(xmlsource);
  xdoc.Active := true;
  Root := xdoc.DocumentElement;
  n1:=Root.ChildNodes[0];
  n2:=n1.ChildNodes.First;
  showmessage(n2.text);
end;

解决方案 »

  1.   

    xmlsource,是xml的字符串,类似于
    xmlsource:='<?xml version="1.0" encoding="utf-8" ?><n><n1>adfad</n1></n>';把xdoc.LoadFromXML(xmlsource);
    改为,xdoc.loadfromfile('c:\xx.xml');
    也是一样的效果,在函数中出错,在onclick事件中,就没问题郁闷啊。
      

  2.   

    function xmltohtml(xmlsource:string):string;
    var
    Root,n1,n2,data:IXMLNode;
    xdoc:TXMLDocument;   ----------->  IXMLDocument;
    begin
    xdoc := TXMLDocument.Create(nil);
    xdoc.LoadFromXML(xmlsource);
      xdoc.Active := true;
      Root := xdoc.DocumentElement;
      n1:=Root.ChildNodes[0];
      n2:=n1.ChildNodes.First;
      showmessage(n2.text);
    end;
      

  3.   

    要引用这4个单元: xmldom, XMLIntf, msxmldom, XMLDoc
      

  4.   

    搞定了,谢谢大家热心帮忙.
    function xmltohtml(xmlsource:string):string;
    var
    Root,n1,n2,data:IXMLNode;
    xdoc:IXMLDocument;
    begin
    xdoc := NewXMLDocument(nil);
    CoInitialize(nil);
    xdoc.LoadFromXML(xmlsource);
    Root := xdoc.DocumentElement;
    n1:=Root.ChildNodes[0];
    n2:=n1.ChildNodes.First;
    end;
     xdoc:=nil;
     CoUninitialize();