Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to save a record to a XML file without saving the fields separately?

Tags:

xml

record

delphi

I have a big record which is consist of many fields with different types and also dynamic arrays. I want to save it to a file and then read it back. Imagine this simple record:

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

Normally I have to use something like this to save this record to a file:

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

But it doesn't work because of 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 field-by-field using addChild())

like image 668
Mahm00d Avatar asked May 04 '11 14:05

Mahm00d


People also ask

How do I save as an XML file?

Click File > Save As, and select the location where you want to save the file. , point to the arrow next to Save As, and then click Other Formats. In the File name box, type a name for the XML data file. In the Save as type list, click XML Data, and click Save.

What is an XML data file?

To summarize: An XML file is a file used to store data in the form of hierarchical elements. Data stored in XML files can be read by computer programs with the help of custom tags, which indicate the type of element.

How can we access the data from XML elements?

Retrieving information from XML files by using the Document Object Model, XmlReader class, XmlDocument class, and XmlNode class. Synchronizing DataSet data with XML via the XmlDataDocument class. Executing XML queries with XPath and the XPathNavigator class.

How do I save an XML file in Notepad?

To save the XML document, on the File menu, click Save. To exit XML Notepad, on the File menu, click Exit.


1 Answers

Your XML library obviously needs to know what the fields' values are, or else it cannot serialize them, so you're going to have to go "through all the fields" at some point. Whether you do it manually or have some way of traversing your data structure automatically (like with RTTI) makes no difference. Something has to look at all the fields.

like image 98
Rob Kennedy Avatar answered Sep 30 '22 07:09

Rob Kennedy