Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.IO;
- using System.Text;
- using System.Xml;
- using System.Xml.Serialization;
- namespace XMLSerialization
- {
- class Program
- {
- static void Main(string[] args)
- {
- // deserialize to string
- #region int
- object inObj = 5;
- #endregion
- #region string
- //object inObj = "Testing123";
- #endregion
- #region list
- //List inObj = new List();
- //inObj.Add("0:25");
- //inObj.Add("1:26");
- #endregion
- string[] stringArray = SerializeObjectToXmlString(inObj);
- #region include leading ???
- //int indexOfBracket = stringArray[0].IndexOf('<');
- //stringArray[0] = "??" + stringArray[0];
- #endregion
- #region strip out leading ???
- //int indexOfBracket = stringArray[0].IndexOf('<');
- //string trimmedString = stringArray[0].Substring(indexOfBracket);
- //stringArray[0] = trimmedString;
- #endregion
- Console.WriteLine("Input");
- Console.WriteLine("-----");
- Console.WriteLine("Object Type: " + stringArray[1]);
- Console.WriteLine();
- Console.WriteLine("XML String: " + Environment.NewLine + stringArray[0]);
- Console.WriteLine(String.Empty);
- // serialize back to object
- object outObj = DeserializeXmlStringToObject(stringArray[0], stringArray[1]);
- Console.WriteLine("Output");
- Console.WriteLine("------");
- #region int
- Console.WriteLine("Object: " + (int)outObj);
- #endregion
- #region string
- //Console.WriteLine("Object: " + (string)outObj);
- #endregion
- #region list
- //string[] tempArray;
- //List list = (List)outObj;
- //foreach (string pair in list)
- //{
- // tempArray = pair.Split(':');
- // Console.WriteLine(String.Format("Key:{0} Value:{1}", tempArray[0], tempArray[1]));
- //}
- #endregion
- Console.Read();
- }
- private static string[] SerializeObjectToXmlString(object obj)
- {
- XmlTextWriter writer = new XmlTextWriter(new MemoryStream(), Encoding.UTF8);
- writer.Formatting = Formatting.Indented;
- XmlSerializer serializer = new XmlSerializer(obj.GetType());
- serializer.Serialize(writer, obj);
- MemoryStream stream = (MemoryStream)writer.BaseStream;
- string xmlString = UTF8ByteArrayToString(stream.ToArray());
- string objectType = obj.GetType().FullName;
- return new string[]{xmlString, objectType};
- }
- private static object DeserializeXmlStringToObject(string xmlString, string objectType)
- {
- MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(xmlString));
- XmlSerializer serializer = new XmlSerializer(Type.GetType(objectType));
- object obj = serializer.Deserialize(stream);
- return obj;
- }
- private static string UTF8ByteArrayToString(Byte[] characters)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- return encoding.GetString(characters);
- }
- private static byte[] StringToUTF8ByteArray(String pXmlString)
- {
- UTF8Encoding encoding = new UTF8Encoding();
- return encoding.GetBytes(pXmlString);
- }
- }
- }
- private static string SerializeObjectToXmlString<T>(T obj)
- {
- XmlSerializer xmls = new XmlSerializer(typeof(T));
- using (MemoryStream ms = new MemoryStream())
- {
- XmlWriterSettings settings = new XmlWriterSettings();
- settings.Encoding = Encoding.UTF8;
- settings.Indent = true;
- settings.IndentChars = "t";
- settings.NewLineChars = Environment.NewLine;
- settings.ConformanceLevel = ConformanceLevel.Document;
- using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
- {
- xmls.Serialize(writer, obj);
- }
- string xml = Encoding.UTF8.GetString(ms.ToArray());
- return xml;
- }
- }
- private static T DeserializeXmlStringToObject <T>(string xmlString)
- {
- XmlSerializer xmls = new XmlSerializer(typeof(T));
- using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
- {
- return (T)xmls.Deserialize(ms);
- }
- }
- if (xmlString.Length > 0 && xmlString[0] != '<')
- {
- xmlString = xmlString.Substring(1, xmlString.Length - 1);
- }
- using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
- {
- serializer.Serialize(writer, instance);
- result = writer.ToString();
- }
- object result;
- using (StringReader reader = new StringReader(instance))
- {
- result = serializer.Deserialize(reader);
- }
Add Comment
Please, Sign In to add comment