Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.IO;
- using System.Text;
- using System.Xml.Serialization;
- namespace System.Xml {
- public class XmlHelpers {
- public static object CopyObject(object toCopy) {
- return CopyObject<object>(toCopy);
- }
- /// <summary>
- /// Copies an object using the XmlSerilizer class
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="toCopy"></param>
- /// <returns>New object</returns>
- public static T CopyObject<T>(T toCopy) {
- if(toCopy == null)
- throw new ArgumentNullException("toCopy");
- using(MemoryStream memoryStream = new MemoryStream()) {
- XmlSerializer xs = new XmlSerializer(toCopy.GetType());
- XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
- xmlNamespace.Add(string.Empty,string.Empty);
- xs.Serialize(memoryStream,toCopy,xmlNamespace);
- return (T)xs.Deserialize(memoryStream);
- }
- }
- /// <summary>
- /// Method to reconstruct an Object from XML string
- /// </summary>
- /// <param name="pXmlizedString"></param>
- /// <returns></returns>
- public static Object DeserializeObject(String pXmlizedString,Type objectType) {
- try {
- XmlSerializer xs = new XmlSerializer(objectType);
- using(MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(pXmlizedString))) {
- XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream,Encoding.UTF8);
- return xs.Deserialize(memoryStream);
- }
- }
- catch {
- throw;
- }
- }
- public static bool TryDeserializeObject(string xmlString,Type objType,out object theObject) {
- try {
- theObject = DeserializeObject(xmlString,objType);
- return true;
- }
- catch {
- theObject = null;
- return false;
- }
- }
- public static String SerializeObject(Object pObject) {
- return SerializeObject(pObject,pObject.GetType());
- }
- public static String SerializeObject(Object pObject,Type objectType) {
- try {
- String XmlizedString = null;
- using(MemoryStream memoryStream = new MemoryStream()) {
- XmlSerializer xs = new XmlSerializer(objectType);
- XmlSerializerNamespaces xmlNamespace = new XmlSerializerNamespaces();
- xmlNamespace.Add(string.Empty,string.Empty);
- xs.Serialize(memoryStream,pObject,xmlNamespace);
- XmlizedString = UTF8ByteArrayToString(memoryStream.ToArray());
- }
- return XmlizedString;
- }
- catch {
- throw;
- }
- }
- public static bool TrySerializeObject(object pObject,Type objectType,out string xmlString) {
- try {
- xmlString = SerializeObject(pObject,objectType);
- return true;
- }
- catch {
- xmlString = null;
- return false;
- }
- }
- public static void SaveObjectToFile(object obj,string path) {
- File.WriteAllText(path,SerializeObject(obj,obj.GetType()));
- }
- public static T ReadObjectFromFile<T>(string path) {
- return (T)XmlHelpers.DeserializeObject(File.ReadAllText(path),typeof(T));
- }
- public static object ReadObjectFromFile<T>(string path,Type objType) {
- return XmlHelpers.DeserializeObject(File.ReadAllText(path),objType);
- }
- /// <summary>
- /// To convert a Byte Array of Unicode values (UTF-8 encoded) to a complete String.
- /// </summary>
- /// <param name="characters">Unicode Byte Array to be converted to String</param>
- /// <returns>String converted from Unicode Byte Array</returns>
- private static String UTF8ByteArrayToString(Byte[] characters) {
- UTF8Encoding encoding = new UTF8Encoding();
- String constructedString = encoding.GetString(characters);
- return (constructedString);
- }
- /// <summary>
- /// Converts the String to UTF8 Byte array and is used in De serialization
- /// </summary>
- /// <param name="pXmlString"></param>
- /// <returns></returns>
- private static Byte[] StringToUTF8ByteArray(String pXmlString) {
- UTF8Encoding encoding = new UTF8Encoding();
- Byte[] byteArray = encoding.GetBytes(pXmlString);
- return byteArray;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment