PageRenderTime 64ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSXmlSerialization/ReadMe.txt

#
Plain Text | 58 lines | 43 code | 15 blank | 0 comment | 0 complexity | 1f01bcebcbd65f8327d742a04d69b3c2 MD5 | raw file
  1. ========================================================================
  2. CONSOLE APPLICATION : CSXmlSerialization Project Overview
  3. ========================================================================
  4. /////////////////////////////////////////////////////////////////////////////
  5. Use:
  6. This sample shows how to serialize an in-memory object to a local xml file
  7. and how to deserialize the xml file back to an in-memory object using C#.
  8. The designed MySerializableType includes int, string, generic, as well as
  9. customized type field and property.
  10. /////////////////////////////////////////////////////////////////////////////
  11. Code Logic:
  12. 1. We define a MySerializableType whose instance will be serialized to xml
  13. file. The MySerializableType includes int, string, bool, generic List and
  14. a customized type field/property. Mark the type as [Serializable()].
  15. 2. We define a AnotherType which is used for MySerializableType's inner
  16. customized type. Mark the type as [Serializable()].
  17. 3. In the main method, the codes firstly create and initialize an object of
  18. MySerializableType.
  19. 4. Then it creates a XmlSerializer and StreamWriter to serialize the instance
  20. to local driver as XML file. The generated xml file in step4 looks like,
  21. <?xml version="1.0" encoding="utf-8" ?>
  22. - <MySerializableType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  23. xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  24. <StringValue>Test String</StringValue>
  25. <BoolValue>true</BoolValue>
  26. <IntValue>1</IntValue>
  27. - <AnotherTypeValue>
  28. <StringValue>Inner Test String</StringValue>
  29. <IntValue>2</IntValue>
  30. </AnotherTypeValue>
  31. - <ListValue>
  32. <string>List Item 1</string>
  33. <string>List Item 2</string>
  34. <string>List Item 3</string>
  35. </ListValue>
  36. </MySerializableType>
  37. 5. Then it creates a StreamReader to read and deserialize the xml file back to
  38. object instance
  39. /////////////////////////////////////////////////////////////////////////////
  40. References:
  41. MSDN: XMLSerializer
  42. http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
  43. /////////////////////////////////////////////////////////////////////////////