Click or drag to resize

CTSerializationT Class

Serialization class for CADSharpTools. This class uses the good oldXmlSerializer class.
Inheritance Hierarchy
SystemObject
  CADSharpTools.SerializationCTSerializationT

Namespace:  CADSharpTools.Serialization
Assembly:  CADSharpTools.PDM (in CADSharpTools.PDM.dll) Version: 1.4.16.0 (1.4.16)
Syntax
public class CTSerialization<T>
where T : class

Type Parameters

T
Type

The CTSerializationT type exposes the following members.

Constructors
Methods
  NameDescription
Public methodStatic memberCode exampleDeserialize
De-serializes an XML string into the specified type.
Public methodCode exampleSaveToXML
Saves an object of a specified type to an XML file.
Public methodCode exampleSerialize
Serializes a type into an XML string.
Top
Remarks
CTSerializationT uses XmlSerializer. This imposes limitation on what kind of objects you can serialize. Your implementation must have properties with public setters. To avoid this issue, use CTContractSerializationT.
Examples
This example shows how to use the CTSerialization class.
public class Person
{
   public string FirstName { get; set; }
   public string LastName { get; set; }
   public uint Age { get; set; }
   private Person()
   {
   }
   public override string ToString()
   {
       return string.Format("{0} {1}", this.FirstName, this.LastName);
   }
}
public class Program
{
   static void Main(string[] args)
   {
       // Create new instance of the CTSerialization
       var cTSerializer = new CTSerialization<Person >();
       // Create a new person 
       var Person = new Person() { FirstName = "Amen", LastName = "JLILI", Age = 28 };
       // Serialize the person 
       var serializedPersonReturn = cTSerializer.Serialize(Person);
       if (serializedPersonReturn.IsError)
       {
           System.Diagnostics.Debug.Print(string.Format("Exception caught. Message = {0}", serializedPersonReturn.Exception.Message));
           Console.ReadKey();
           return;
       }
       var serializedPerson = serializedPersonReturn.Value;
       // Print XML 
       System.Diagnostics.Debug.Print(serializedPerson);
       // Deserialize XML 
       var reserializedPersonReturn = CTSerialization<Person>.Deserialize(serializedPerson);
       if (reserializedPersonReturn.IsError)
       {
           System.Diagnostics.Debug.Print(string.Format("Exception caught. Message = {0}", reserializedPersonReturn.Exception.Message));
           Console.ReadKey();
           return;
       }
       var reserializedPerson = reserializedPersonReturn.Value;
       // Print full name 
       System.Diagnostics.Debug.Print(reserializedPerson.ToString());
       // Save to XML file
       string fileName = System.IO.Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), "person.xml");
       cTSerializer.SaveToXML(reserializedPerson, fileName);
       Console.ReadKey();
   }
 }
See Also