SalXML
Namespace: PPJ.Runtime.Xml
Assembly: PPJ.Runtime.50 (5.0.0.0)
Serializes runtime values, including SAL types, to and from the PPJ XML representation.
- C#
- VB.NET
public class SalXML
Public Class SalXML
Serialization includes public fields and readable/writable non-indexed properties unless marked XmlIgnore. Operations close their reader or writer and the associated stream. Exceptions propagate to the caller.
Example:
using PPJ.Runtime;
using PPJ.Runtime.Xml;
public class XmlExample
{
public class Record
{
public SalString Name { get; set; }
}
public static Record RoundTrip(string path)
{
new SalXML(path).Serialize<Record>(new Record { Name = "Ada" });
return new SalXML(path).Deserialize<Record>();
}
}
Constructors
SalXML(stream)
Creates a serializer that reads or writes the supplied stream.
| Name | Type | Description |
|---|---|---|
| stream | Stream | The stream to use. Operations close it when their reader or writer is closed. |
SalXML(filePath)
Creates a serializer that reads or writes an XML file.
| Name | Type | Description |
|---|---|---|
| filePath | String | The file path opened when a serialization operation begins. |
Methods
Deserialize<T>()
Reads a value of a specified type from the configured stream or file.
| Parameter | Type | Description |
|---|---|---|
| T | The requested result type, with a public parameterless constructor. |
Returns: T. The deserialized value, or default(T) if no element is found.
Creates a reader at the current stream position and closes it before returning.
Example:
Round-trip a list using separate output and input streams. The new input stream starts at position zero; Deserialize reads from the current position and closes that stream. An existing seekable input stream must be positioned at the start of the XML document before calling Deserialize.
using System.Collections.Generic;
using System.IO;
using PPJ.Runtime.Xml;
byte[] xml;
using (var output = new MemoryStream())
{
new SalXML(output).Serialize<List<int>>(new List<int> { 101, 205 });
xml = output.ToArray();
}
List<int> restored;
using (var input = new MemoryStream(xml))
{
// A new MemoryStream starts at position zero.
restored = new SalXML(input).Deserialize<List<int>>();
}
// restored contains the saved IDs; input was closed by Deserialize.
Deserialize()
Attempts to infer a value's type from the current reader's first element.
Returns: Object. The deserialized object, or null if no element is found.
This overload does not create a reader. On a new serializer, the reader is null and the call fails. Use Deserialize<T> to initialize and consume the configured input.
Serialize<T>(o)
Writes a value as an indented UTF-16 XML document.
| Parameter | Type | Description |
|---|---|---|
| T | The type used to name the root element. | |
| o | Object | The value to serialize. |
The root element is named from T. The writer is flushed and closed when writing the value completes or fails.
Example:
Serialize a list into bytes. Serialize closes the output stream. MemoryStream.ToArray remains usable after closure, so copy the bytes and open a new stream for any later read.
using System.Collections.Generic;
using System.IO;
using PPJ.Runtime.Xml;
var selectedIds = new List<int> { 101, 205 };
byte[] xml;
using (var output = new MemoryStream())
{
new SalXML(output).Serialize<List<int>>(selectedIds);
xml = output.ToArray();
}
// The closed output stream cannot be rewound; xml can be stored or sent.