Guest User

Untitled

a guest
Jun 25th, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.83 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. using System.Xml;
  5. using System.Xml.Serialization;
  6.  
  7. namespace XMLSerialization
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. // deserialize to string
  14. #region int
  15. object inObj = 5;
  16. #endregion
  17.  
  18. #region string
  19. //object inObj = "Testing123";
  20. #endregion
  21.  
  22. #region list
  23. //List inObj = new List();
  24. //inObj.Add("0:25");
  25. //inObj.Add("1:26");
  26. #endregion
  27.  
  28. string[] stringArray = SerializeObjectToXmlString(inObj);
  29.  
  30. #region include leading ???
  31. //int indexOfBracket = stringArray[0].IndexOf('<');
  32. //stringArray[0] = "??" + stringArray[0];
  33. #endregion
  34.  
  35. #region strip out leading ???
  36. //int indexOfBracket = stringArray[0].IndexOf('<');
  37. //string trimmedString = stringArray[0].Substring(indexOfBracket);
  38. //stringArray[0] = trimmedString;
  39. #endregion
  40.  
  41. Console.WriteLine("Input");
  42. Console.WriteLine("-----");
  43. Console.WriteLine("Object Type: " + stringArray[1]);
  44. Console.WriteLine();
  45. Console.WriteLine("XML String: " + Environment.NewLine + stringArray[0]);
  46. Console.WriteLine(String.Empty);
  47.  
  48. // serialize back to object
  49. object outObj = DeserializeXmlStringToObject(stringArray[0], stringArray[1]);
  50.  
  51. Console.WriteLine("Output");
  52. Console.WriteLine("------");
  53.  
  54. #region int
  55. Console.WriteLine("Object: " + (int)outObj);
  56. #endregion
  57.  
  58. #region string
  59. //Console.WriteLine("Object: " + (string)outObj);
  60. #endregion
  61.  
  62. #region list
  63. //string[] tempArray;
  64. //List list = (List)outObj;
  65.  
  66. //foreach (string pair in list)
  67. //{
  68. // tempArray = pair.Split(':');
  69. // Console.WriteLine(String.Format("Key:{0} Value:{1}", tempArray[0], tempArray[1]));
  70. //}
  71. #endregion
  72.  
  73. Console.Read();
  74. }
  75.  
  76. private static string[] SerializeObjectToXmlString(object obj)
  77. {
  78. XmlTextWriter writer = new XmlTextWriter(new MemoryStream(), Encoding.UTF8);
  79. writer.Formatting = Formatting.Indented;
  80. XmlSerializer serializer = new XmlSerializer(obj.GetType());
  81. serializer.Serialize(writer, obj);
  82.  
  83. MemoryStream stream = (MemoryStream)writer.BaseStream;
  84. string xmlString = UTF8ByteArrayToString(stream.ToArray());
  85.  
  86. string objectType = obj.GetType().FullName;
  87.  
  88. return new string[]{xmlString, objectType};
  89. }
  90.  
  91. private static object DeserializeXmlStringToObject(string xmlString, string objectType)
  92. {
  93. MemoryStream stream = new MemoryStream(StringToUTF8ByteArray(xmlString));
  94. XmlSerializer serializer = new XmlSerializer(Type.GetType(objectType));
  95.  
  96. object obj = serializer.Deserialize(stream);
  97.  
  98. return obj;
  99. }
  100.  
  101. private static string UTF8ByteArrayToString(Byte[] characters)
  102. {
  103. UTF8Encoding encoding = new UTF8Encoding();
  104. return encoding.GetString(characters);
  105. }
  106.  
  107. private static byte[] StringToUTF8ByteArray(String pXmlString)
  108. {
  109. UTF8Encoding encoding = new UTF8Encoding();
  110. return encoding.GetBytes(pXmlString);
  111. }
  112.  
  113.  
  114. }
  115. }
  116.  
  117. private static string SerializeObjectToXmlString<T>(T obj)
  118. {
  119. XmlSerializer xmls = new XmlSerializer(typeof(T));
  120. using (MemoryStream ms = new MemoryStream())
  121. {
  122. XmlWriterSettings settings = new XmlWriterSettings();
  123. settings.Encoding = Encoding.UTF8;
  124. settings.Indent = true;
  125. settings.IndentChars = "t";
  126. settings.NewLineChars = Environment.NewLine;
  127. settings.ConformanceLevel = ConformanceLevel.Document;
  128.  
  129. using (XmlWriter writer = XmlTextWriter.Create(ms, settings))
  130. {
  131. xmls.Serialize(writer, obj);
  132. }
  133.  
  134. string xml = Encoding.UTF8.GetString(ms.ToArray());
  135. return xml;
  136. }
  137. }
  138.  
  139. private static T DeserializeXmlStringToObject <T>(string xmlString)
  140. {
  141. XmlSerializer xmls = new XmlSerializer(typeof(T));
  142.  
  143. using (MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(xmlString)))
  144. {
  145. return (T)xmls.Deserialize(ms);
  146. }
  147. }
  148.  
  149. if (xmlString.Length > 0 && xmlString[0] != '<')
  150. {
  151. xmlString = xmlString.Substring(1, xmlString.Length - 1);
  152. }
  153.  
  154. using (StringWriter writer = new StringWriter(CultureInfo.InvariantCulture))
  155. {
  156. serializer.Serialize(writer, instance);
  157. result = writer.ToString();
  158. }
  159.  
  160. object result;
  161. using (StringReader reader = new StringReader(instance))
  162. {
  163. result = serializer.Deserialize(reader);
  164. }
Add Comment
Please, Sign In to add comment