Can I save a record to an XML file without saving the fields separately?

I have a large record, which consists of many fields with different types, as well as with dynamic arrays. I want to save it to a file and then read it. Imagine this simple entry:

TCustomRecord = Record Field1 : array of integer; Field2 : Integer; Field3 : String; end; 

Usually I should use something like this to save this entry in a file:

 var f : File of TCustomRecord; cr : TCustomeRecord; begin Write(f, cr); end; 

But this does not work due to dynamic array and string type.

So the question is:

Is there a way to save (export) it to a TXMLDocument without going through all the fields? (I mean adding fields after field using addChild() )

+7
source share
2 answers

Obviously, your XML library needs to know what the values ​​of the fields are, or it cannot serialize them, so you will need to “go through all the fields” at some point. Regardless of whether you do it manually or somehow move your data structure automatically (for example, with RTTI), there is no difference. Something must look at all the fields.

+3
source

I will carefully say “No,” although I have no idea what you mean by “going through all the fields.”

Another answer refers to a workable way of repeating fields in your record using RTTI. Then you must make sure that your entries will not use any type that your RTTI-iterator code does not understand, or that it will not be saved.

Since “you don’t need to call AddChild yourself,” you can just store everything in XML attributes without any child nodes. I don’t know if you know about it, but there’s not only one way to map the fields in the record to the possible XML content that might turn out.

Here is an example entry:

 TMyData = record Name:String; Age:Integer; Money:Double; end; 

If you don’t want to call AddChild at all, you can simply write the attributes by creating one object as follows:

  <MyData Name="Warren" Age="48" Money="100.00" /> 

Nobody says there that you should even use child nodes:

  <MyData> <Name>Warren</Name> <Age>48</Age> <Money>100.00<Money/> </MyData> 

So, it seems to me that you are avoiding the fact that XML provides you with many ways to do this, and that the MSXML DOM API, as an API, gives you several ways to map any record to a file.

+1
source

All Articles