.NET에서 Serialization은 객체를 저장하고, 객체를 다시 불러오는데 매우 유용하다. 그리고 네트워크로 Serialization된 객체를 전송하고,
다시 그 객체를 사용할 수 도 있는 .NET에서 핵심적인 기능중 하나이다.
XmlSerialization을 하기 위해 가장 유의할 점은 생성자는 input parameter가 없는 생성자만이 가능하다는 점을 항시 알아야지 된다.
input parameter가 있는 경우에는 Runtime시에 exception이 발생되게 된다.
다시 그 객체를 사용할 수 도 있는 .NET에서 핵심적인 기능중 하나이다.
XmlSerialization을 하기 위해 가장 유의할 점은 생성자는 input parameter가 없는 생성자만이 가능하다는 점을 항시 알아야지 된다.
input parameter가 있는 경우에는 Runtime시에 exception이 발생되게 된다.
001.
/****************************** Module Header ******************************\
002.
* Module Name: Program.cs
003.
* Project: CSXmlSerialization
004.
* Copyright (c) Microsoft Corporation.
005.
*
006.
* This sample shows how to serialize an in-memory object to a local xml file
007.
* and how to deserialize the xml file back to an in-memory object using
008.
* C#. The designed MySerializableType includes int, string, generic, as well
009.
* as customized type field and property.
010.
*
011.
* This source is subject to the Microsoft Public License.
012.
* See http://www.microsoft.com/opensource/licenses.mspx#Ms-PL.
013.
* All other rights reserved.
014.
*
015.
* History:
016.
* * 7/29/2009 3:00 PM Colbert Zhou Created
017.
* * 8/20/2009 12:01 AM Jialiang Ge Reviewed
018.
\***************************************************************************/
019.
020.
#region Using directives
021.
using
System;
022.
using
System.Collections.Generic;
023.
using
System.Linq;
024.
using
System.Text;
025.
using
System.Xml;
026.
using
System.Xml.Serialization;
027.
using
System.IO;
028.
using
System.Collections;
029.
#endregion
030.
031.
032.
namespace
CSXmlSerialization
033.
{
034.
class
Program
035.
{
036.
static
void
Main(
string
[] args)
037.
{
038.
/////////////////////////////////////////////////////////////////
039.
// Serialize the object to an XML file.
040.
//
041.
042.
// Create and initialize a MySerializableType instance.
043.
MySerializableType instance =
new
MySerializableType();
044.
instance.BoolValue =
true
;
045.
instance.IntValue = 1;
046.
instance.StringValue =
"Test String"
;
047.
instance.ListValue.Add(
"List Item 1"
);
048.
instance.ListValue.Add(
"List Item 2"
);
049.
instance.ListValue.Add(
"List Item 3"
);
050.
instance.AnotherTypeValue =
new
AnotherType();
051.
instance.AnotherTypeValue.IntValue = 2;
052.
instance.AnotherTypeValue.StringValue =
"Inner Test String"
;
053.
054.
// Create the serializer
055.
XmlSerializer serializer =
new
XmlSerializer(
typeof
(MySerializableType));
056.
057.
// Serialize the object to an XML file
058.
using
(StreamWriter streamWriter = File.CreateText(
059.
"CSXmlSerialization.xml"
))
060.
{
061.
serializer.Serialize(streamWriter, instance);
062.
}
063.
064.
065.
/////////////////////////////////////////////////////////////////
066.
// Deserialize from a XML file to an object instance.
067.
//
068.
069.
// Deserialize the object
070.
MySerializableType deserializedInstance;
071.
using
(StreamReader streamReader = File.OpenText(
072.
"CSXmlSerialization.xml"
))
073.
{
074.
deserializedInstance = serializer.Deserialize(streamReader)
075.
as
MySerializableType;
076.
}
077.
078.
// Dump the object
079.
Console.WriteLine(
"BoolValue: {0}"
, deserializedInstance.BoolValue);
080.
Console.WriteLine(
"IntValue: {0}"
, deserializedInstance.IntValue);
081.
Console.WriteLine(
"StringValue: {0}"
, deserializedInstance.StringValue);
082.
Console.WriteLine(
"AnotherTypeValue.IntValue: {0}"
,
083.
deserializedInstance.AnotherTypeValue.IntValue);
084.
Console.WriteLine(
"AnotherTypeValue.StringValue: {0}"
,
085.
deserializedInstance.AnotherTypeValue.StringValue);
086.
Console.WriteLine(
"ListValue: "
);
087.
foreach
(
object
obj
in
deserializedInstance.ListValue)
088.
{
089.
Console.WriteLine(obj.ToString());
090.
}
091.
}
092.
}
093.
094.
095.
/// <summary>
096.
/// Serializable Type Declaration
097.
/// </summary>
098.
[Serializable()]
099.
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.
}
167.
</
string
></
string
></
string
>