Hello. I have the class. The properties in the class are written in a certain order. It is necessary to write in XML these properties with values, exactly in the order in which they follow each other in the description:
TRequestBody = class(TBaseRequestBody)
public
//function SaveToString(const RequestData: TDomNode): string; override;
procedure ToXML(const XMLNode: TDomNode);override;
published
property ProtocolLabel;
property ProtocolVersion;
property RequestId;
property DateTime;
property Command;
end;
I'm trying to do this through RTTI:
procedure TRequestBody.ToXML(const XMLNode: TDomNode);
var
PropList: TPropInfoList;
i: integer;
PI : PPropInfo;
PT : PTypeInfo;
xDoc: TXMLDocument;
RootNode, ParentNode, Node: TDomNode;
begin
xDoc := TXMLDocument.Create;
try
RootNode := xDoc.CreateElement('RequestBody');
xDoc.AppendChild(RootNode);
RootNode := xDoc.DocumentElement;
PropList := TPropInfoList.Create(Self,tkProperties);
for i := 0 to PropList.Count - 1 do
begin
PI := PropList.Items[i];
PT:=PI^.PropType;
//Str := Pi^.Name;
ParentNode := xDoc.CreateElement(Pi^.Name);
case PT^.Kind of
tkInteger:
Node := xDoc.CreateTextNode(IntToStr(GetOrdProp(Self,PI)));
tkSString,
tkLString,
tkAString:
Node := xDoc.CreateTextNode(GetStrProp(Self,PI));
tkFloat :
if (PT=TypeInfo(TDateTime)) then
Node := xDoc.CreateTextNode(DateTimeToStr(GetFloatProp(Self,PI)));
end;
ParentNode.AppendChild(Node);
RootNode.AppendChild(ParentNode);
//ParentNode.NodeValue:=;
end;
finally
XMLNode.AppendChild(RootNode.CloneNode(true,XMLNode.OwnerDocument));
xDoc.Free;
end;
end;
In the output I need XML with the filed order like class properies order:
<RequestBody>
<ProtocolLabel>labelxxx</ProtocolLabel>
<ProtocolVersion>1.1</ProtocolVersion>
<RequestId>{9ED60922-23E8-4DD8-94C7-6AC2C2A82570}</RequestId>
<DateTime>26.03.2017 12:52:41</DateTime>
<Command>1</Command>
</RequestBody>
But, with the RTTI I get the output:
<RequestBody>
<Command>1</Command>
<DateTime>26.03.2017 12:52:41</DateTime>
<ProtocolLabel>labelxxx</ProtocolLabel>
<ProtocolVersion>1.1</ProtocolVersion>
<RequestId>{9ED60922-23E8-4DD8-94C7-6AC2C2A82570}</RequestId>
</RequestBody>
i.e. class fields in the RTTI is sorted? How I can disable properties of the class sorting in the rtti?