Click or drag to resize

CTContractSerializationTSaveToXML Method

Saves an object of a specified type to an XML file.

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

Parameters

obj
Type: T
Object to serialize.
fileName
Type: SystemString
complete pathname of the file to be saved.

Return Value

Type: MethodReturnBoolean
MethodReturn of the specified type. Use Value to get the returned object.
Examples
This example shows how to use the CTContractSerializationT class.
public class Person
{
   [DataMember]
   public string FirstName { get; private set; }
   [DataMember]
   public string LastName { get; private set; }
   [DataMember]
   public uint Age { get; private set; }
   private Person()
   {
   }
   public static Person CreateInstance()
   {
      var p = new Person;
      p.LastName = "Jlili";
      p.FirstName = "Amen";
      p.Age = 29;
      return p;
   }
   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 CTContractSerialization
       var cTSerializer = new CTContractSerialization<Person >();
       // Create a new person 
       var Person = Person.CreateInstance();
       // 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 = CTContractSerialization<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