DataContractManagerによるオブジェクトのXML Serialize


## 前提
`System.Runtime.Serialization.dll`を参照に追加する。

## XML Serializer

`BaseType`で指定したクラスをXML形式に変換するための`XmlSerializer`クラスを用意する。

```c#:XmlSerializer.cs
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Xml;

namespace XmlSerialize
{
	/// <summary>
	/// XML Serializer Static Class
	/// </summary>
	public static class XmlSerializer<BaseType>
	{
		/// <summary>
		/// Save Object to XML File
		/// </summary>
		/// <param name="fname">File Name</param>
		/// <param name="obj">Object</param>
		public static void SaveXml(string fname, BaseType obj)
		{
			/* Create Data Contract Serializer */
			var dcs = new DataContractSerializer(typeof(BaseType));
			/* Create XML Writer Settings */
			var xws = new XmlWriterSettings() {
				NewLineHandling = NewLineHandling.None,
				Indent = true,
				IndentChars = "    ",
				Encoding = new System.Text.UTF8Encoding(false)
			};
			/* Create XML Writer */
			using(var xw = XmlWriter.Create(fname, xws))
			{
				/* Write Object to XML */
				dcs.WriteObject(xw, obj);
			}
		}

		/// <summary>
		/// Load Object from XML
		/// </summary>
		/// <returns>Object</returns>
		/// <param name="fname">File Name</param>
		public static BaseType LoadXml(string fname)
		{
			/* Create Data Contract Serializer */
			var dcs = new DataContractSerializer(typeof(BaseType));
			/* Create XML Reader */
			using(var xr = XmlReader.Create(fname))
			{
				/* Read Object from XML */
				return (BaseType)dcs.ReadObject(xr);
			}
		}
	}
}
```

## Data Object

XMLでSerializeする対象のクラスを用意する。
クラス属性として`[DataContract]`、Serialize対象メンバには`[DataMember]`属性を適用する。

- `[DataMember]`属性が適用されていないメンバはSerializeされない
- `[DataMember]`属性が適用されていれば、privateなメンバでもSerializeされる
- `System.Xml.Serialization.XmlSerializer`ではSerializeできない`Dictionary`クラスなどもSerialize可能


```c#:DataObject.cs
using System;
using System.Collections.Generic;
using System.Runtime.SerializAation;

namespace XmlSerialize
{
    /// <summary>
    /// Data Object Class
    /// </summary>
    [DataContract]
    public class DataObject
    {
    	 /// <summary>
        /// Primitive Data Member
        /// </summary>
        [DataMember]
        public int Data1 { get; private set; }
        
        /// <summary>
        /// Ignored Data Member
        /// </summary>
        public int Data2 { get; private set; }
        
        /// <summary>
        /// Private Data Member
        /// </summary>
        [DataMember]
        private int Data3 { get; private set; }
    	
        /// <summary>
        /// List Data Member
        /// </summary>
        [DataMember]
        public List<string> StringList { get; private set; }
        
        /// <summary>
        /// Dictionary Data Member
        /// </summary>
        [DataMember]
        public Dictionary<string,int> IntDictionary { get; private set; }
        
        /// <summary>
        /// Constructor
        /// </summary>
        public DataObject()
        {
        	this.StringList = new List<string>();
        	this.IntDictionary = new Dictionary<string,int>();
        }
    }
}
```

## Usage

```c#:Main.cs
using System;

namespace XmlSerialize
{
	class MainClass
	{
		public static void Main(string[] args)
		{
			/* Create Data */
			var data1 = new DataObject();
			
			/* Save Data to XML */
			XmlSerializer<DataObject>.Save("Object.xml", data1);
			
			/* Load Data from XML */
			var data2 = XmlSerializer<DataObject>Load("Object.xml");
		}
	}
}
```