Created
July 16, 2012 15:22
-
-
Save rpgmaker/3123301 to your computer and use it in GitHub Desktop.
C# Code for XmlSerializer Generator
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 static string Serialize<T>(T obj) { | |
var sb = new StringBuilder(DEFAULT_SB_CAPACITY); | |
using (var writer = XmlWriter.Create(sb, | |
new XmlWriterSettings() { OmitXmlDeclaration = true })) { | |
WriteObject(obj, Normalize(obj.GetType().Name), writer); | |
writer.Flush(); | |
return sb.ToString(); | |
} | |
} | |
public static object Deserialize(string xml, Type type) { | |
using (var ms = new MemoryStream()) { | |
using (var sw = new StreamWriter(ms)) { | |
sw.Write(xml); | |
sw.Flush(); | |
ms.Position = 0; | |
using (var reader = XmlReader.Create(ms)) { | |
reader.Read(); | |
return Deserialize(reader, type); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment