PageRenderTime 29ms CodeModel.GetById 4ms RepoModel.GetById 0ms app.codeStats 0ms

/Visual Studio 2008/CSXmlSerialization/Program.cs

#
C# | 166 lines | 106 code | 19 blank | 41 comment | 0 complexity | 3a83b9ea51ae0615863952071756524a MD5 | raw file
  1. /****************************** Module Header ******************************\
  2. * Module Name: Program.cs
  3. * Project: CSXmlSerialization
  4. * Copyright (c) Microsoft Corporation.
  5. *
  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
  8. * C#. The designed MySerializableType includes int, string, generic, as well
  9. * as customized type field and property.
  10. *
  11. * This source is subject to the Microsoft Public License.
  12. * See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
  13. * All other rights reserved.
  14. *
  15. * History:
  16. * * 7/29/2009 3:00 PM Colbert Zhou Created
  17. * * 8/20/2009 12:01 AM Jialiang Ge Reviewed
  18. \***************************************************************************/
  19. #region Using directives
  20. using System;
  21. using System.Collections.Generic;
  22. using System.Linq;
  23. using System.Text;
  24. using System.Xml;
  25. using System.Xml.Serialization;
  26. using System.IO;
  27. using System.Collections;
  28. #endregion
  29. namespace CSXmlSerialization
  30. {
  31. class Program
  32. {
  33. static void Main(string[] args)
  34. {
  35. /////////////////////////////////////////////////////////////////
  36. // Serialize the object to an XML file.
  37. //
  38. // Create and initialize a MySerializableType instance.
  39. MySerializableType instance = new MySerializableType();
  40. instance.BoolValue = true;
  41. instance.IntValue = 1;
  42. instance.StringValue = "Test String";
  43. instance.ListValue.Add("List Item 1");
  44. instance.ListValue.Add("List Item 2");
  45. instance.ListValue.Add("List Item 3");
  46. instance.AnotherTypeValue = new AnotherType();
  47. instance.AnotherTypeValue.IntValue = 2;
  48. instance.AnotherTypeValue.StringValue = "Inner Test String";
  49. // Create the serializer
  50. XmlSerializer serializer = new XmlSerializer(typeof(MySerializableType));
  51. // Serialize the object to an XML file
  52. using (StreamWriter streamWriter = File.CreateText(
  53. "CSXmlSerialization.xml"))
  54. {
  55. serializer.Serialize(streamWriter, instance);
  56. }
  57. /////////////////////////////////////////////////////////////////
  58. // Deserialize from a XML file to an object instance.
  59. //
  60. // Deserialize the object
  61. MySerializableType deserializedInstance;
  62. using (StreamReader streamReader = File.OpenText(
  63. "CSXmlSerialization.xml"))
  64. {
  65. deserializedInstance = serializer.Deserialize(streamReader)
  66. as MySerializableType;
  67. }
  68. // Dump the object
  69. Console.WriteLine("BoolValue: {0}", deserializedInstance.BoolValue);
  70. Console.WriteLine("IntValue: {0}", deserializedInstance.IntValue);
  71. Console.WriteLine("StringValue: {0}", deserializedInstance.StringValue);
  72. Console.WriteLine("AnotherTypeValue.IntValue: {0}",
  73. deserializedInstance.AnotherTypeValue.IntValue);
  74. Console.WriteLine("AnotherTypeValue.StringValue: {0}",
  75. deserializedInstance.AnotherTypeValue.StringValue);
  76. Console.WriteLine("ListValue: ");
  77. foreach (object obj in deserializedInstance.ListValue)
  78. {
  79. Console.WriteLine(obj.ToString());
  80. }
  81. }
  82. }
  83. /// <summary>
  84. /// Serializable Type Declaration
  85. /// </summary>
  86. [Serializable()]
  87. public class MySerializableType
  88. {
  89. // String field and property
  90. private string stringValue;
  91. public string StringValue
  92. {
  93. get { return stringValue; }
  94. set { stringValue = value; }
  95. }
  96. // Bool field and property
  97. private bool boolValue;
  98. public bool BoolValue
  99. {
  100. get { return boolValue; }
  101. set { boolValue = value; }
  102. }
  103. // Int field and property
  104. private int intValue;
  105. public int IntValue
  106. {
  107. get { return intValue; }
  108. set { intValue = value; }
  109. }
  110. // Another type field and property
  111. private AnotherType anotherTypeValue;
  112. public AnotherType AnotherTypeValue
  113. {
  114. get { return anotherTypeValue; }
  115. set { anotherTypeValue = value; }
  116. }
  117. // Generic type field and property
  118. private List<string> listValue = new List<string>();
  119. public List<string> ListValue
  120. {
  121. get { return listValue; }
  122. set { listValue = value; }
  123. }
  124. // Ignore a field using NonSerialized attribute
  125. [NonSerialized()]
  126. private int ignoredField = 1;
  127. }
  128. /// <summary>
  129. /// Another Type Declaration
  130. /// </summary>
  131. [Serializable()]
  132. public class AnotherType
  133. {
  134. private string stringValue;
  135. public string StringValue
  136. {
  137. get { return stringValue; }
  138. set { stringValue = value; }
  139. }
  140. private int intValue;
  141. public int IntValue
  142. {
  143. get { return intValue; }
  144. set { intValue = value; }
  145. }
  146. }
  147. }