Thursday 26 November 2009

Xml Serialization and Deserialization using XSD

Preparation:
  1. Get an XSD file
  2. Generate the Classes from XSD using xsd.exe tool
Xml Serialization Process:
  1. Create a new instance of the object from the XSD e.g. login object
  2. Use XmlSerializer class to serialize the object and get the result back; you would just need some Factory methods
  3. Use the serialized data
Xml Deserialization Process:
  1. Get the xml as string
  2. Specify the type e.g. login
  3. Deserlialize the xml to the type specified using XmlSerializer class
  4. Use the deserialized object
Examples:
 [TestMethod]
       [Ignore]
       public void Should_Generate_Xsd_For_Class()
       {
 
           const string Xsd = @"""C:\Program Files (x86)\Microsoft SDKs\Windows\v8.1A\bin\NETFX 4.5.1 Tools\xsd.exe""";
 
           // /t:{2}  /outputdir:{3}
           var arguments = string.Format("{0} /t:{1}  /outputdir:{2}",
               @"""C:\Tfs\Project\Trunk\Project\bin\Debug\ProjectName.dll""",
               "Namespace.ClassName",
               @"C:\Test");
          
           Process.Start(Xsd, arguments);
       } 

No comments: