Search content within the blog

Wednesday, May 5, 2010

XML Serialization Code

The following code serializes a object and stores it in c drive(C:).
Note: if you are using IIS 5 then set permission to write to that file or folder by adding computername\ASAPNET as one of the user for that file or folder . IF using IIS 6 then add "NETWORK SERVICE" as a user to the file or folder.

Code
// Create a new XmlSerializer instance with the type of the test class
XmlSerializer SerializerObj = new XmlSerializer(typeof(JobSpecification));
// Create a new file stream to write the serialized object to a file
TextWriter WriteFileStream = new StreamWriter(@"C:\createjobspec.xml");
SerializerObj.Serialize(WriteFileStream, jobSpec);
// Cleanup
WriteFileStream.Close();

here "JobSpecification" is a class defined and jobspec is an object of that class.