Summary
TXmlSerializer is a specialized TSerializer class type for writing values in XML format, using standard VCL XML classes, such as TXmlDocument.
Syntax
TXmlSerializer = class(TSerializer)
Constructors
Name | Description | |
Create(IXMLDocument) | ||
Create(IXMLNode) |
Properties
Methods
Name | Description | |
DoBeginArray | ||
DoBeginObject | ||
DoEndArray | ||
DoEndObject | ||
DoWrite |
Remarks
TXmlSerializer object should be created providing a reference to existing IXMLNode object into which all serializing data will be written. Each root-level data value will be written as a child of provided IXMLNode object. TXmlSerializer creates new IXmlNode for every serializing value using IXmlNode.AddChild method; thus every serializing value will be added as last child of the parent node, respecting ordered nature of XML documents.
Every element in XML document should have a name (sometimes called "tag"). Since core serializer API does not support root-level values naming, additional API has been provided: NG.Serialization.Xml.TXmlSerializer.RootValue(string) property should be used before actual writing of any root-level value (otherwise, invalid state exception will be thrown):
Delphi | Copy Code |
---|---|
szr.RootValue['MyValue'].Value(7); |
Since, NG.Serialization.Xml.TXmlSerializer.RootValue(string) is a default indexed property, its property name can be suppressed to make code more readable:
Delphi | Copy Code |
---|---|
szr['MyValue'].Value(7); |
If a value need to be serialized as a root document node, TXmlDocument.Node can be specified as a constructor argument while creating TXmlSerializer object:
Delphi | Copy Code |
---|---|
xml := TXmlDocument.Create(nil); xml.Active := True; szr := TXmlSerializer.Create(xml.Node); try srz['Book'].Value<TBook>(book); finally srz.Free; end; |
Xml | Copy Code |
---|---|
<Book> <Tittle>Delphi XE Handbook</Tittle> <Author>Marco Cantu</Author> </Book> |
Examples
Delphi | Copy Code |
---|---|
xml := TXmlDocument.Create(nil); xml.Options := xml.Options + [doNodeAutoIndent]; xml.Active := True; rootNode := xml.ChildNodes['Books']; szr := TXmlSerializer.Create(rootNode); try srz['Book'].Value<TBook>(book1); srz['Book'].Value<TBook>(book2); finally szr.Free; end; xml.SaveToFile('Books.xml'); |
See Also
Reference
- NG.Serialization.Xml.TXmlDeserializer
- TSerializer
- TDeserializer
- NG.Serialization.Xml Namespace