尝试获取xml节点时访问冲突

时间:2013-07-13 22:13:38

标签: xml delphi root access-violation txmldocument

我尝试开发一个帮助程序来读/写并控制TXMLDocument的实例。 我为这项工作写了一个简单的单元。该单元具有将实例分配给全局变量的过程,并为文档控制设置一些变量。 单位是:

unit Globals;

{ Variables globales de la aplicacion, con sus correspondientes accessors }

interface

uses
  { XML Helper }
  xmldom, XMLIntf, msxmldom, XMLDoc, SysUtils, DateUtils;

type
  XmlCheckPoint = Record
    asociado: boolean;
    xmlFile: TXMLDocument;
    saved: boolean;
    lastModification: TDateTime;
    lastSave: TDateTime;
    path: TFilename;
  End;

  { Firmas }
  procedure assignXml(var aXml: TXMLDocument);
  procedure xmlWriteProyectoNode(obra,cliente,ubicacion,fecha,sondeo,estudio: String);
  function existsXml(): boolean;
  function xmlIsUpdated(): boolean;

var
  Xml: XmlCheckPoint;

程序assignXml,工作正常:

  procedure assignXml(var aXml: TXMLDocument);
  begin
    Xml.xmlFile := aXml;
    Xml.asociado := true;
    Xml.saved := false;
    Xml.lastSave := Yesterday;
    Xml.path := '';
    { Inserto el nodo raiz }
    Xml.xmlFile.Active := true;
    Xml.xmlFile.AddChild('raiz');
    Xml.lastModification := Now();
  end;

但是,xmlWriteProyectoNode(...)会爆炸应用程序:

procedure xmlWriteProyectoNode(obra,cliente,ubicacion,fecha,sondeo,estudio: String);
  var
    root,meta,child: IXMLNode;
  begin
    Xml.xmlFile.Active := true;
    root := Xml.xmlFile.DocumentElement;
    meta := root.AddChild('proyecto');
    child := meta.AddChild('obra');
    child.Text := obra;
      [...]
    Xml.lastModification := Now();
  end;

当invoques writeXmlProyectoNode(...)出现访问冲突错误时,应用程序崩溃。在执行时间。 Embarcadero debuger说冲突线是:

root := Xml.xmlFile.DocumentElement;

我需要获取根元素,并认为这是正确的方法...... 我最新的Delphi,有什么想法? 谢谢!。

编辑: XML创建(newXml类型为TXMLDocument

newXml := TXMLDocument.Create(nil);
newXml.Options := [doNodeAutoIndent];
newXml.Active := true;
{ Asocio la instancia de XMLDocument a mi variable global newXml}
Globals.assignXml(newXml);
相关问题