PageRenderTime 6ms CodeModel.GetById 2ms app.highlight 1ms RepoModel.GetById 1ms 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/////////////////////////////////////////////////////////////////////////////
 6Use:
 7
 8This sample shows how to serialize an in-memory object to a local xml file 
 9and how to deserialize the xml file back to an in-memory object using C#. 
10The designed MySerializableType includes int, string, generic, as well as 
11customized type field and property.
12
13
14/////////////////////////////////////////////////////////////////////////////
15Code Logic:
16
171. We define a MySerializableType whose instance will be serialized to xml 
18file. The MySerializableType includes int, string, bool, generic List and 
19a customized type field/property. Mark the type as [Serializable()].
20
212. We define a AnotherType which is used for MySerializableType's inner 
22customized type. Mark the type as [Serializable()].
23
243. In the main method, the codes firstly create and initialize an object of
25MySerializableType.
26
274. Then it creates a XmlSerializer and StreamWriter to serialize the instance
28to local driver as XML file. The generated xml file in step4 looks like,
29
30  <?xml version="1.0" encoding="utf-8" ?> 
31- <MySerializableType xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
32     xmlns:xsd="http://www.w3.org/2001/XMLSchema">
33  <StringValue>Test String</StringValue> 
34  <BoolValue>true</BoolValue> 
35  <IntValue>1</IntValue> 
36- <AnotherTypeValue>
37  <StringValue>Inner Test String</StringValue> 
38  <IntValue>2</IntValue> 
39  </AnotherTypeValue>
40- <ListValue>
41  <string>List Item 1</string> 
42  <string>List Item 2</string> 
43  <string>List Item 3</string> 
44  </ListValue>
45  </MySerializableType>
46
475. Then it creates a StreamReader to read and deserialize the xml file back to
48object instance
49
50
51/////////////////////////////////////////////////////////////////////////////
52References:
53
54MSDN: XMLSerializer
55http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
56
57
58/////////////////////////////////////////////////////////////////////////////