Serialization usage - SmartPlant Foundation - IM Update 44 - Customization & Programming - Hexagon

SmartPlant Foundation Customization

Language
English
Product
SmartPlant Foundation
Search by Category
Customization & Programming
SmartPlant Foundation / SDx Version
10

Two serialization techniques are available using the SmartPlant Foundation Server. The first uses an XML Document object to facilitate the return of the Server API result objects to the client. The second uses a stream object instead of an XML Document for performance reasons. The second technique is particularly useful for large data sets where the loading of the XML Document for returning to the client can often take longer than the Server API itself.

The following example shows the conventional technique, using an XML Document:

'

' Create the reply node, append it to the response dom.

'

Dim lnodReply As XmlNode = CoreModule.Server.Response.AppendChild(Me.CoreModule.Server.Response.CreateElement("Reply"))

'

' Serialise a collection, appending the result to the xml document.

'

lnodReply.AppendChild(Me.CoreModule.Server.Response.ImportNode(Me.CoreModule.Serializer.Serialize(mobjCollection, New EFXmlSerializationWriter(mobjColumnSet)), True))

The code above is fine in most cases, but sometimes we would want to avoid the whole XML result set being loading into an XML document, for performance reasons.

The following example shows the technique for serializing large data sets.

'

' Create a new response stream, this can be a memory or file stream,

' if a file stream is used, it will be automatically closed and

' deleted after serialisation is complete.

'

Me.CoreModule.Server.ResponseStream = New IO.FileStream(IO.Path.GetTempFileName, FileMode.OpenOrCreate)

'

' Create a new xml writer.

'

Dim lobjXmlWriter As XmlWriter = XmlWriter.Create(Me.CoreModule.Server.ResponseStream, New XmlWriterSettings)

'

' Start the xml return (you must still create a valid xml document).

'

lobjXmlWriter.WriteStartElement("Reply")

'

' Serialise the collection. Note the lack of a result node, the xml is written directly to the stream.

'

Me.CoreModule.Serializer.Serialize(lobjXmlWriter, mobjReportResults, New EFXmlSerializationWriter(mobjColumnSet))

'

' Write the end element and close the writer.

'

lobjXmlWriter.WriteEndElement()

lobjXmlWriter.Close()

Note that both techniques are perfectly valid, but consider serializing using the stream technique where you're expecting a large data set to be returned from your Server API.