Created
December 26, 2012 15:08
-
-
Save rohinomiya/4380846 to your computer and use it in GitHub Desktop.
Dictionaryをシリアル化する(XMLファイルに保存) ref: http://qiita.com/items/b88a5da3965a1c5bed0d
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
XmlSerializer serializer = new XmlSerializer(typeof(RecordInfo)); | |
RecordInfo record2 = new RecordInfo(); | |
using (FileStream fs = new FileStream(GetXMLFilePath(), FileMode.Open)) | |
{ | |
record2 = serializer.Deserialize(fs) as RecordInfo; | |
// XML からオブジェクトが復元されている | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class FieldInfo | |
{ | |
public FieldInfo(string FieldName, string JapaneseFieldName) | |
{ | |
this.FieldName = FieldName; | |
this.JapaneseFieldName = JapaneseFieldName; | |
} | |
public string FieldName; | |
public string JapaneseFieldName; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private void Form1_Load(object sender, EventArgs e) | |
{ | |
record = new RecordInfo(); | |
record.Add("key1", new FieldInfo("key1", "キー1")); | |
record.Add("key2", new FieldInfo("key2", "キー2")); | |
} | |
private string GetXMLFilePath() | |
{ | |
return @"d:\test.xml"; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// これは私が必要だったので作ったクラス。作成する必要はない | |
public class RecordInfo: SerializableDictionary<string, FieldInfo> | |
{ | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// シリアル化可能な Dictionary クラス | |
public class SerializableDictionary<Tkey, TValue> : Dictionary<Tkey, TValue>, IXmlSerializable | |
{ | |
public System.Xml.Schema.XmlSchema GetSchema() | |
{ | |
return null; | |
} | |
public void ReadXml(XmlReader reader) | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(KeyValue)); | |
reader.Read(); | |
if (reader.IsEmptyElement) | |
return; | |
while (reader.NodeType != XmlNodeType.EndElement) | |
{ | |
KeyValue kv = serializer.Deserialize(reader) as KeyValue; | |
if (kv != null) | |
Add(kv.Key, kv.Value); | |
} | |
reader.Read(); | |
} | |
public void WriteXml(XmlWriter writer) | |
{ | |
XmlSerializer serializer = new XmlSerializer(typeof(KeyValue)); | |
foreach (var key in Keys) | |
{ | |
serializer.Serialize(writer, new KeyValue(key, this[key])); | |
} | |
} | |
public class KeyValue | |
{ | |
public KeyValue(Tkey key, TValue value) | |
{ | |
Key = key; | |
Value = value; | |
} | |
public Tkey Key { get; set; } | |
public TValue Value { get; set; } | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var selializer = new XmlSerializer(typeof(RecordInfo)); | |
using (FileStream fs = new FileStream(GetXMLFilePath(), FileMode.Create)) | |
{ | |
selializer.Serialize(fs, record); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment