xmldelphidom

Is Delphi XML binding really not allowing to read a ""-valued attribute?


I have XML stanza:

<Parameter Name="report_code" Value=""/>

with (automatically generated) Delphi XML binding:

  TXMLParameterType = class(TXMLNode, IXMLParameterType)
  protected
    { IXMLParameterType }
    function Get_Name: WideString;
    function Get_Value: WideString;
    procedure Set_Name(Value: WideString);
    procedure Set_Value(Value: WideString);
  end;
    
function TXMLParameterType.Get_Name: WideString;
begin
  Result := AttributeNodes['Name'].Text;
end;
    
procedure TXMLParameterType.Set_Name(Value: WideString);
begin
  SetAttribute('Name', Value);
end;
    
function TXMLParameterType.Get_Value: WideString;
begin
  Result := AttributeNodes['Value'].NodeValue;
end;
    
procedure TXMLParameterType.Set_Value(Value: WideString);
begin
  SetAttribute('Value', Value);
end;

And I see in XMLDoc.pas that the NodeValue getter does this:

function TXMLNode.GetNodeValue: OleVariant;
begin
  Result := GetText;
  if Result = '' then
    Result := Null;
end;

That results in an error for my business code:

if Param.HasAttribute('Value') then
  Result := Param.Value;

This is beyond comprehension - if there is a ""-valued attribute Value, then one should really be able to read this "" value.

Can't this behavior be configured in Delphi (judging from the Delphi XML code - no)? Is it standard XML behavior?

I checked and such Delphi XMLDoc code was present both in the older (Delphi 2009) and more recent (Delphi XE 11) versions.


Solution

  • As you noted, the NodeValue property getter will return a Null variant if the value is empty. You can use VarToWideStr() to account for that, which will return an empty string if the variant is Null, eg:

    uses
      ..., System.Variants;
    
    function TXMLParameterType.Get_Value: WideString;
    begin
      Result := VarToWideStr(AttributeNodes['Value'].NodeValue);
    end;