Click or drag to resize

CTSerializationTSerialize Method

Serializes a type into an XML string.

Namespace:  CADSharpTools.Serialization
Assembly:  CADSharpTools.PDM (in CADSharpTools.PDM.dll) Version: 1.4.16.0 (1.4.16)
Syntax
public MethodReturn<string> Serialize(
	T obj
)

Parameters

obj
Type: T

Return Value

Type: MethodReturnString
MethodReturn of type String. Use Value to get the returned object.
Remarks
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