无法将ixmlnode强制转换为从ixmlnode继承的其他接口

时间:2019-09-16 14:36:21

标签: xml delphi interface casting

我正在尝试使用接口构造结构,因此可以执行x = A.B而不是A.ChildNodes ['B']。问题是,每次我尝试将childnode强制转换为相关接口时,都会给我一个EIntfCastError。

unit TestXML;
interface
uses
    Winapi.Windows,
    Winapi.Messages,
    System.SysUtils,
    System.Variants,
    System.Classes,
    Vcl.Graphics,
    Vcl.Controls,
    Vcl.Forms,
    Vcl.Dialogs,
    Xml.XMLDoc,
    Xml.XMLIntf,
    Vcl.StdCtrls;
type
    IXMLKmehrHeaderType = interface(IXMLNode)
        ['{4FC29DEE-A5A8-4F8E-AE59-6EB42F966B2D}']
    end;
    TXMLTransactionResponseType = class(TXMLNode, IXMLNode)
    protected
        function Get_Response: IXMLNode;
        function Get_Acknowledge: IXMLNode;
    public
        procedure AfterConstruction; override;
    end;
    TXMLKmehrHeaderType = class(TXMLNode, IXMLKmehrHeaderType)
    end;
    IXMLGetTransactionListResponseType = interface(IXMLNode)
        ['{D07A4789-034A-4EEF-80FE-11EB95EB5124}']
        function Get_KmehrHeader: IXMLKmehrHeaderType;
        property KmehrHeader: IXMLKmehrHeaderType read Get_KmehrHeader;
    end;
    TXMLGetTransactionListResponseType = class(TXMLTransactionResponseType, IXMLGetTransactionListResponseType)
    protected
        function Get_KmehrHeader: IXMLKmehrHeaderType;
    public
        procedure AfterConstruction; override;
    end;
    TTest = class(TForm)
        btnTest: TButton;
        txtXML: TMemo;
        procedure btnTestClick(Sender: TObject);
    private
        { Private declarations }
    public
        { Public declarations }
    end;
var
    Test: TTest;
implementation
{$R *.dfm}
{ TXMLTransactionResponseType }
procedure TXMLTransactionResponseType.AfterConstruction;
var
    prefix: string;
begin
    inherited;
    prefix := FindNamespaceDecl('http://www.ehealth.fgov.be/hubservices/core/v3').LocalName;
    RegisterChildNode(prefix + ':ackknowledge', TXMLNode);
    RegisterChildNode(prefix + ':response', TXMLNode);
end;
function TXMLTransactionResponseType.Get_Acknowledge: IXMLNode;
begin
    Result := ChildNodes['ackknowledge'] as IXMLNode;
end;
function TXMLTransactionResponseType.Get_Response: IXMLNode;
begin
    Result := ChildNodes['response'] as IXMLNode;
end;
{ TXMLGetTransactionListResponseType }
procedure TXMLGetTransactionListResponseType.AfterConstruction;
var
    prefix: string;
begin
    inherited;
    prefix := FindNamespaceDecl('http://www.ehealth.fgov.be/hubservices/core/v3').LocalName;
    RegisterChildNode(prefix + ':kmehrheader', TXMLKmehrHeaderType);
end;
function TXMLGetTransactionListResponseType.Get_KmehrHeader: IXMLKmehrHeaderType;
var
    prefix: string;
begin
    prefix := FindNamespaceDecl('http://www.ehealth.fgov.be/hubservices/core/v3').LocalName;
    Result := ChildNodes[prefix + ':kmehrheader'] as IXMLKmehrHeaderType;
end;
procedure TTest.btnTestClick(Sender: TObject);
var
    doc: IXMLDocument;
    Test: IXMLGetTransactionListResponseType;
    header: IXMLKmehrHeaderType;
    prefix: string;
begin
    doc := LoadXMLData(txtXML.Text);
    Test := doc.GetDocBinding('GetTransactionListResponse', TXMLGetTransactionListResponseType, 'http://www.ehealth.fgov.be/hubservices/protocol/v3') as IXMLGetTransactionListResponseType;
    prefix := Test.FindNamespaceDecl('http://www.ehealth.fgov.be/hubservices/core/v3').LocalName;
    header := Test.KmehrHeader;
end;
end.

我希望能够将一个子节点转换为TXMLKmehrHeaderType。 但是每次,我都会收到异常EIntfCastError,并且消息接口不受支持。'

请帮助,整日都在尝试解决此问题。

0 个答案:

没有答案