Last active
April 11, 2017 12:28
-
-
Save lavn0/798d72ce7a2fd86050d6453aab896039 to your computer and use it in GitHub Desktop.
DataContractParser
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
| // add reference to System.Runtime.Serialization, Version=4.0.0.0 | |
| // add reference to System.XML, Version=4.0.0.0 | |
| using System.IO; | |
| using System.Runtime.Serialization; | |
| using System.Text; | |
| public static class DataContractParser | |
| { | |
| public static T ReadObject<T>(Stream stream) | |
| { | |
| return (T)new DataContractSerializer(typeof(T)).ReadObject(stream); | |
| } | |
| public static T ReadObject<T>(string fileName) | |
| { | |
| using (var sr = new StreamReader(fileName)) | |
| { | |
| return ReadObject<T>(sr.BaseStream); | |
| } | |
| } | |
| public static void WriteObject<T>(T obj, Stream stream) | |
| { | |
| new DataContractSerializer(typeof(T)).WriteObject(stream, obj); | |
| } | |
| public static void WriteObject<T>(T obj, string fileName) | |
| { | |
| using (var sw = new StreamWriter(fileName)) | |
| { | |
| WriteObject<T>(obj, sw.BaseStream); | |
| } | |
| } | |
| public static T ParseObject<T>(string xmlString) | |
| { | |
| using (var ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString))) | |
| { | |
| return ReadObject<T>(ms); | |
| } | |
| } | |
| public static string ParseObject<T>(T obj) | |
| { | |
| MemoryStream ms = null; | |
| try | |
| { | |
| ms = new MemoryStream(); | |
| WriteObject<T>(obj, ms); | |
| ms.Seek(0, SeekOrigin.Begin); | |
| using (var sr = new StreamReader(ms)) | |
| { | |
| ms = null; | |
| return sr.ReadToEnd(); | |
| } | |
| } | |
| finally | |
| { | |
| ms?.Dispose(); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment