Explain how to use XML to deserialize an object.
- Deserialization is the process of taking XML-formatted data and converting it to a .NET framework object.
- The XmlSerializer class is used to deserialize the string to an instance of the Test class.
- To obtain a suitable stream that can be passed into the XmlSerializer's constructor, a StringReader (from the System.IO namespace) is declared.
Example:
FileStream fStream = new FileStream(filename, FileMode.Open);
XmlReader rdr = new XmlTextReader(fStream);
Class1 cls1;
cls1 = (Class1) srl.Deserialize(rdr);
- The XML stream is saved to a file and the same file is then read back and reconstructed into a copy of the original object using the Deserialize method.
- To deserialize the objects, call the Deserialize method with the FileStream as an argument.