Last active
December 18, 2015 07:09
-
-
Save adarapata/5744196 to your computer and use it in GitHub Desktop.
xml尻洗い図
This file contains 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
using System.Collections; | |
using System.Xml; | |
using System.Xml.Serialization; | |
using System.IO; | |
/// <summary> | |
/// クラスオブジェクトとxmlをシリアライズ・デシリアライズするクラス | |
/// </summary> | |
public static class MyXmlSerializer | |
{ | |
/// <summary> | |
/// オブジェクトをxmlにシリアライズする | |
/// </summary> | |
/// <param name="classObject"></param> | |
/// <param name="fileName"></param> | |
public static void Serialize(object classObject, string fileName) | |
{ | |
XmlSerializer serial = new XmlSerializer(classObject.GetType()); | |
FileStream fileStream = new FileStream(fileName, FileMode.Create); | |
serial.Serialize(fileStream, classObject); | |
fileStream.Close(); | |
} | |
/// <summary> | |
/// xmlファイルをオブジェクトにデシリアライズする | |
/// </summary> | |
/// <typeparam name="T"></typeparam> | |
/// <param name="xmlText"></param> | |
/// <returns></returns> | |
public static T Desirialize<T>(string xmlText) | |
{ | |
XmlSerializer serial = new XmlSerializer(typeof(T)); | |
XmlReader reader = XmlReader.Create(new System.IO.StringReader(xmlText)); | |
T classObject = (T)serial.Deserialize(reader); | |
return classObject; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment