我在尝试访问XMLDocument的文档元素时遇到访问冲突错误。我正在根据某个文件的存在创建XMLDocument的对象。 错误消息:访问XMLDocument中的文档元素时发生访问冲突
项目PROJECT1.EXE“模块访问冲突在地址0047B152‘引发的异常类EAccessViolation与消息地址B1D59357的project1.exe'.Read’
以下是我的代码:
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未被初始化?请帮忙。
代码中断在32行和根在公共部分中声明 – XPecto
当我测试一个实际的XML文档时,我得到'无效指针操作'在没有特定的断点,之后构造函数完成。 –
当没有XML文档并且它产生单个节点' '时,代码运行正常,但是当再次运行时,它会中断任何对文档元素的读取尝试。 –
XPecto