首页 > 解决方案 > 在 Delphi 中使用 XML 数据绑定向导创建具体的 XML 文件

问题描述

我想在 Delphi 10.4 和 XML 数据绑定向导中使用编辑字段来创建具体数据。

我有 XML 模式,我用它在 Delphi 中创建了下面的代码。

你能帮我用下面的代码来创建具体的 XML 文件吗?

这是我从数据绑定向导中得到的:

function NewUnknown: IXMLGetBalance_Type;
begin
  Result := NewXMLDocument.GetDocBinding('Unknown', TXMLGetBalance_Type, TargetNamespace) as IXMLGetBalance_Type;
end;

{ TXMLGetBalance_Type }

procedure TXMLGetBalance_Type.AfterConstruction;
begin
  RegisterChildNode('consent', TXMLGetBalance_Type_consent);
  inherited;
end;

function TXMLGetBalance_Type.Get_Consent: IXMLGetBalance_Type_consent;
begin
  Result := ChildNodes['consent'] as IXMLGetBalance_Type_consent;
end;

{ TXMLGetBalance_Type_consent }

function TXMLGetBalance_Type_consent.Get_Type_: UnicodeString;
begin
  Result := ChildNodes['type'].Text;
end;

procedure TXMLGetBalance_Type_consent.Set_Type_(Value: UnicodeString);
begin
  ChildNodes['type'].NodeValue := Value;
end;

function TXMLGetBalance_Type_consent.Get_Target: UnicodeString;
begin
  Result := ChildNodes['target'].Text;
end;

procedure TXMLGetBalance_Type_consent.Set_Target(Value: UnicodeString);
begin
  ChildNodes['target'].NodeValue := Value;
end;

function TXMLGetBalance_Type_consent.Get_Id: UnicodeString;
begin
  Result := ChildNodes['id'].Text;
end;

procedure TXMLGetBalance_Type_consent.Set_Id(Value: UnicodeString);
begin
  ChildNodes['id'].NodeValue := Value;
end;

如何使用此代码使用来自编辑字段的数据创建具体的 XML 文件?

标签: xmldelphi

解决方案


只需调用NewUnknown(),根据需要为返回的属性赋值IXMLGetBalance_Type,然后将其保存到文件中。例如:

uses
  ..., UnitGeneratedByXMLWizard;

procedure TMyForm.DoSomething;
var
  Unk: IXMLGetBalance_Type;
begin
  Unk := NewUnknown;
  Unk.Consent.Type_ := ...;
  Unk.Consent.Target := ...;
  Unk.Consent.Id := ...;
  Unk.SaveToFile('path_to\myfile.xml');
end;

查看 Embarcadero 的文档了解更多详细信息:

使用 XML 数据绑定向导生成的代码


推荐阅读