在XMLDocument中使用DocumentElement时发生访问冲突 [英] Access violation when using DocumentElement In XMLDocument

查看:169
本文介绍了在XMLDocument中使用DocumentElement时发生访问冲突的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试使用 XMLDocument DocumentElement 时,总是遇到访问冲突。我根据文件的存在创建了 XMLDocument

I always get an access violation when I try to use the DocumentElement of the XMLDocument. I create XMLDocument based on the existence of some file.

错误消息


项目project1.exe引发异常类EAccessViolation并带有
消息'模块
中地址0047B152的访问冲突'project1.exe '。读取地址B1D59357'

Project project1.exe raised exception class EAccessViolation with message 'Access violation at address 0047B152 in module 'project1.exe'.Read of Address B1D59357'

我的代码

unit XMLBase;

interface
uses
  SysUtils, xmldom, XMLIntf, XMLDoc, Forms;

type
  TXMLbase = class
  private
    { Private declarations }
  public
    XMLDocument1: TXMLDocument;
    root: IXMLNode;    
    constructor Create;
  end;

var
  fn: string;

implementation

constructor TXMLbase.Create;
begin   
  fn := ChangeFileExt(Application.ExeName, '.xml');
  XMLDocument1 := TXMLDocument.Create(nil);
  XMLDocument1.Options := [doNodeAutoIndent];
  XMLDocument1.Active := False;
  //optional, is used to indent the Xml document
  if FileExists(fn) then
  begin
  XMLDocument1.LoadFromFile(fn);
  XMLDocument1.Active:= True;
  root := XMLDocument1.DocumentElement;  //<<--- Access Voilation
  end
  else
  begin
    XMLDocument1.Active := False;
    XMLDocument1.XML.Text := '';
    XMLDocument1.Active := True;
    root := XMLDocument1.AddChild('Settings');    
  end;
XMLDocument1.SaveToFile(fn);
end;

end.

由于对象或指针的初始化不正确而导致访问冲突,这是否意味着 XMLDocument 是否未初始化?

Access violations arise due to improper initialization of object or pointers, does that mean that the XMLDocument is not being initialized?

推荐答案

如David所示,只是将XMLDocument1声明从 XMLDocument1:TXMLDocument XMLDocument1:IXMLDocument 解决了该问题。您的问题与使用TXmlDocument

As indicated by David just changing your XMLDocument1 declaration from XMLDocument1: TXMLDocument to XMLDocument1: IXMLDocument solves the problem. Your issue is related to Using TXmlDocument

这篇关于在XMLDocument中使用DocumentElement时发生访问冲突的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆