PageRenderTime 17ms CodeModel.GetById 10ms app.highlight 4ms RepoModel.GetById 1ms 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
 20#region Using directives
 21using System;
 22using System.Collections.Generic;
 23using System.Linq;
 24using System.Text;
 25using System.Xml;
 26using System.Xml.Serialization;
 27using System.IO;
 28using System.Collections;
 29#endregion
 30
 31
 32namespace CSXmlSerialization
 33{
 34    class Program
 35    {
 36        static void Main(string[] args)
 37        {
 38            /////////////////////////////////////////////////////////////////
 39            // Serialize the object to an XML file.
 40            // 
 41
 42            // Create and initialize a MySerializableType instance.
 43            MySerializableType instance = new MySerializableType();
 44            instance.BoolValue = true;
 45            instance.IntValue = 1;
 46            instance.StringValue = "Test String";
 47            instance.ListValue.Add("List Item 1");
 48            instance.ListValue.Add("List Item 2");
 49            instance.ListValue.Add("List Item 3");
 50            instance.AnotherTypeValue = new AnotherType();
 51            instance.AnotherTypeValue.IntValue = 2;
 52            instance.AnotherTypeValue.StringValue = "Inner Test String";
 53
 54            // Create the serializer
 55            XmlSerializer serializer = new XmlSerializer(typeof(MySerializableType));
 56
 57            // Serialize the object to an XML file
 58            using (StreamWriter streamWriter = File.CreateText(
 59                "CSXmlSerialization.xml"))
 60            {
 61                serializer.Serialize(streamWriter, instance);
 62            }
 63
 64
 65            /////////////////////////////////////////////////////////////////
 66            // Deserialize from a XML file to an object instance.
 67            // 
 68
 69            // Deserialize the object
 70            MySerializableType deserializedInstance;
 71            using (StreamReader streamReader = File.OpenText(
 72                "CSXmlSerialization.xml"))
 73            {
 74                deserializedInstance = serializer.Deserialize(streamReader) 
 75                    as MySerializableType;
 76            }
 77
 78            // Dump the object
 79            Console.WriteLine("BoolValue: {0}", deserializedInstance.BoolValue);
 80            Console.WriteLine("IntValue: {0}", deserializedInstance.IntValue);
 81            Console.WriteLine("StringValue: {0}", deserializedInstance.StringValue);
 82            Console.WriteLine("AnotherTypeValue.IntValue: {0}", 
 83                deserializedInstance.AnotherTypeValue.IntValue);
 84            Console.WriteLine("AnotherTypeValue.StringValue: {0}", 
 85                deserializedInstance.AnotherTypeValue.StringValue);
 86            Console.WriteLine("ListValue: ");
 87            foreach (object obj in deserializedInstance.ListValue)
 88            {
 89                Console.WriteLine(obj.ToString());
 90            }
 91        }
 92    }
 93
 94
 95    /// <summary>
 96    /// Serializable Type Declaration
 97    /// </summary>
 98    [Serializable()]
 99    public class MySerializableType
100    {
101        // String field and property
102        private string stringValue;
103        public string StringValue
104        {
105            get { return stringValue; }
106            set { stringValue = value; }
107        }
108
109        // Bool field and property
110        private bool boolValue;
111        public bool BoolValue
112        {
113            get { return boolValue; }
114            set { boolValue = value; }
115        }
116
117        // Int field and property
118        private int intValue;
119        public int IntValue
120        {
121            get { return intValue; }
122            set { intValue = value; }
123        }
124
125        // Another type field and property
126        private AnotherType anotherTypeValue;
127        public AnotherType AnotherTypeValue
128        {
129            get { return anotherTypeValue; }
130            set { anotherTypeValue = value; }
131        }
132
133        // Generic type field and property
134        private List<string> listValue = new List<string>();
135        public List<string> ListValue
136        {
137            get { return listValue; }
138            set { listValue = value; }
139        }
140
141        // Ignore a field using NonSerialized attribute
142        [NonSerialized()]
143        private int ignoredField = 1;
144    }
145
146    /// <summary>
147    /// Another Type Declaration
148    /// </summary>
149    [Serializable()]
150    public class AnotherType
151    {
152        private string stringValue;
153        public string StringValue
154        {
155            get { return stringValue; }
156            set { stringValue = value; }
157        }
158
159        private int intValue;
160        public int IntValue
161        {
162            get { return intValue; }
163            set { intValue = value; }
164        }
165    }
166}