andrew4582

XmlHelpers

May 7th, 2012
189
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.80 KB | None | 0 0
  1. using System.IO;
  2. using System.Text;
  3. using System.Xml.Serialization;
  4.  
  5. namespace System.Xml {
  6.  
  7.     public class XmlHelpers {
  8.  
  9.         public static object CopyObject(object toCopy) {
  10.             return CopyObject<object>(toCopy);
  11.         }
  12.         /// <summary>
  13.         /// Copies an object using the XmlSerilizer class
  14.         /// </summary>
  15.         /// <typeparam name="T"></typeparam>
  16.         /// <param name="toCopy"></param>
  17.         /// <returns>New object</returns>
  18.         public static T CopyObject<T>(T toCopy) {
  19.             if(toCopy == null)
  20.                 throw new ArgumentNullException("toCopy");
  21.  
  22.             using(MemoryStream memoryStream = new MemoryStream()) {
  23.                 XmlSerializer xs = new XmlSerializer(toCopy.GetType());
  24.                 XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
  25.  
  26.                 xmlNamespace.Add(string.Empty,string.Empty);
  27.                 xs.Serialize(memoryStream,toCopy,xmlNamespace);
  28.                 return (T)xs.Deserialize(memoryStream);
  29.             }
  30.         }
  31.         /// <summary>
  32.         /// Method to reconstruct an Object from XML string
  33.         /// </summary>
  34.         /// <param name="pXmlizedString"></param>
  35.         /// <returns></returns>
  36.         public static Object DeserializeObject(String pXmlizedString,Type objectType) {
  37.             try {
  38.                 XmlSerializer xs = new XmlSerializer(objectType);
  39.                 using(MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString))) {
  40.                     XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
  41.                     return xs.Deserialize(memoryStream);
  42.                 }
  43.             }
  44.             catch {
  45.                 throw;
  46.             }
  47.         }
  48.         public static bool TryDeserializeObject(string xmlString,Type objType,out object theObject) {
  49.             try {
  50.                 theObject = DeserializeObject(xmlString,objType);
  51.                 return true;
  52.             }
  53.             catch {
  54.                 theObject = null;
  55.                 return false;
  56.             }
  57.         }
  58.  
  59.         public static String SerializeObject(Object pObject) {
  60.             return SerializeObject(pObject,pObject.GetType());
  61.         }
  62.  
  63.         public static String SerializeObject(Object pObject,Type objectType) {
  64.             try {
  65.                 String XmlizedString = null;
  66.  
  67.                 using(MemoryStream memoryStream = new MemoryStream()) {
  68.                     XmlSerializer xs = new XmlSerializer(objectType);
  69.  
  70.                     XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
  71.                     xmlNamespace.Add(string.Empty,string.Empty);
  72.  
  73.                     xs.Serialize(memoryStream,pObject,xmlNamespace);
  74.                     XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
  75.                 }
  76.                 return XmlizedString;
  77.             }
  78.             catch {
  79.                 throw;
  80.             }
  81.         }
  82.  
  83.         public static bool TrySerializeObject(object pObject,Type objectType,out string xmlString) {
  84.             try {
  85.                 xmlString = SerializeObject(pObject,objectType);
  86.                 return true;
  87.             }
  88.             catch {
  89.                 xmlString = null;
  90.                 return false;
  91.             }
  92.         }
  93.  
  94.         public static void SaveObjectToFile(object obj,string path) {
  95.             File.WriteAllText(path,SerializeObject(obj,obj.GetType()));
  96.         }
  97.  
  98.         public static T ReadObjectFromFile<T>(string path) {
  99.             return (T)XmlHelpers.DeserializeObject(File.ReadAllText(path),typeof(T));
  100.         }
  101.  
  102.         public static object ReadObjectFromFile<T>(string path,Type objType) {
  103.             return XmlHelpers.DeserializeObject(File.ReadAllText(path),objType);
  104.         }
  105.         /// <summary>
  106.         /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
  107.         /// </summary>
  108.         /// <param name="characters">Unicode Byte Array to be converted to String</param>
  109.         /// <returns>String converted from Unicode Byte Array</returns>
  110.         private static String UTF8ByteArrayToString(Byte[] characters) {
  111.             UTF8Encoding encoding = new UTF8Encoding();
  112.             String constructedString = encoding.GetString(characters);
  113.             return (constructedString);
  114.         }
  115.  
  116.         /// <summary>
  117.         /// Converts the String to UTF8 Byte array and is used in De serialization
  118.         /// </summary>
  119.         /// <param name="pXmlString"></param>
  120.         /// <returns></returns>
  121.         private static Byte[] StringToUTF8ByteArray(String pXmlString) {
  122.             UTF8Encoding encoding = new UTF8Encoding();
  123.             Byte[] byteArray = encoding.GetBytes(pXmlString);
  124.             return byteArray;
  125.         }
  126.     }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment