Some nice conversion methods.

Note: Everyone handles Exceptions differently therefore I haven't added try{} catch{} finally{} to these methods.  These are just ways I have had to use and have found on the web.  Then when generics were introduced I added all the generic references.

It's 4 sets of conversions

Object To Stream.
  public static byte[] ObjectToStream<T>(T item)
        {
            MemoryStream str = new MemoryStream();
            BinaryFormatter bf = new BinaryFormatter();
            bf.Serialize(str, item);
            return str.ToArray();
        }
Stream to Object
   public static T StreamToObject<T>(byte[] str)
        {
            MemoryStream mem = new MemoryStream(str);
            BinaryFormatter bf = new BinaryFormatter();
            mem.Position = 0;
            return (T)bf.Deserialize(mem);
        }
Object To String  
        public static string ObjectToString<T>(T item)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            using (StringWriter writer = new StringWriter())
            {
                serializer.Serialize(writer, item);
                return writer.ToString();
            }
        }

String To Object

        public static T StringToObject<T>(string s)
        {
            XmlDocument xdoc = new XmlDocument()
            xdoc.LoadXml(s);
            return XmlDocumentToObject<T>(xdoc);
        }
Object To XML Document
        public static XmlDocument ObjectToXMLDocument<T>(T item)
        {
            XmlDocument xdoc = new XmlDocument();
            using (MemoryStream str = new MemoryStream())
            {
                XmlSerializer serializer = new XmlSerializer(typeof(T));
                XmlTextWriter writer = new XmlTextWriter(str, new UTF8Encoding());
                serializer.Serialize(writer, item);
                xdoc.LoadXml(ASCIIEncoding.UTF8.GetString(str.ToArray()));
                return xdoc;
            }
        }

XML Document To Object
        public static T XmlDocumentToObject<T>(XmlDocument xdoc)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            XmlNodeReader reader = new XmlNodeReader(xdoc.DocumentElement);
            return (T)serializer.Deserialize(reader);
        }
String to XMLDocument
       //For string to XmlDocument use (XmlDocument).LoadXML(string)
        public static XmlDocument StringToXmlDocument(string s)
        {
            XmlDocument xdoc = new XmlDocument();
            xdoc.LoadXml(s);
            return xdoc;
        }

XMLDocument To String

       //For XMLDocument to string use (XmlDocument).OuterXml()
        public static string XmlDocumentToString(XmlDocument xdoc)
        {
            return xdoc.OuterXml;
        }
______________________________________________________________________________________

Using
Testing Method
public static void RunEachTest<T>(string dir, string file, T item)
        {
            try
            {   //Start XDoc
                XmlDocument  xdoc = new XmlDocument();
                xdoc.Load(dir + file);
                //XmlDocument to String
                string s = Messages.Helper.SerializationHelper.XmlDocumentToString(xdoc);
                //XmlDocument to Object
                item = Messages.Helper.SerializationHelper.XmlDocumentToObject<T>(xdoc);
                //Test Object To Stream
                byte[] b = Messages.Helper.SerializationHelper.ObjectToStream<T>(item);
                //Test Stream to Object
                item = Messages.Helper.SerializationHelper.StreamToObject<T>(b);
                //Object to String
                s = Messages.Helper.SerializationHelper.ObjectToString<T>(item);
                //Object to XmlDocument
                xdoc = Messages.Helper.SerializationHelper.ObjectToXMLDocument<T>(item);
                //String to Object
                item = Messages.Helper.SerializationHelper.StringToObject<T>(s);
                //String to XmlDocument
                xdoc.LoadXml(s);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
            return;
        }

Categories: Biztalk-WorkFlow

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: