在 Delphi 中使用 XML 数据绑定向导创建具体的 XML 文件

Creating concrete XML files with XML Data Binding Wizard in Delphi

我想在 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 文件?

只需调用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 的文档了解更多详情:

Using Code That the XML Data Binding Wizard Generates