activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jgo...@apache.org
Subject svn commit: r820832 - /activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs
Date Thu, 01 Oct 2009 22:59:22 GMT
Author: jgomes
Date: Thu Oct  1 22:59:22 2009
New Revision: 820832

URL: http://svn.apache.org/viewvc?rev=820832&view=rev
Log:
Change default XML serializing to UTF-8 format.  Added overloaded function for specifying
different encoding format.

Modified:
    activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs

Modified: activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs?rev=820832&r1=820831&r2=820832&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs Thu Oct  1
22:59:22 2009
@@ -18,6 +18,7 @@
 using System;
 using System.IO;
 using System.Text;
+using System.Xml;
 using System.Xml.Serialization;
 
 namespace Apache.NMS.Util
@@ -29,36 +30,59 @@
 	{
 		public static string Serialize(object obj)
 		{
-			XmlSerializer serializer = new XmlSerializer(obj.GetType());
-			StringWriter writer = new StringWriter(new StringBuilder(4096));
+			return Serialize(obj, Encoding.UTF8);
+		}
 
-			/*
-			 * If the XML document has been altered with unknown 
-			 * nodes or attributes, handle them with the 
-			 * UnknownNode and UnknownAttribute events.
-			 */
-			serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
-			serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
-			serializer.Serialize(writer, obj);
-			return writer.ToString();
+		public static string Serialize(object obj, Encoding encoding)
+		{
+			try
+			{
+				MemoryStream memoryStream = new MemoryStream();
+				XmlSerializer serializer = new XmlSerializer(obj.GetType());
+				XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, encoding);
+
+				/*
+				 * If the XML document has been altered with unknown
+				 * nodes or attributes, handle them with the
+				 * UnknownNode and UnknownAttribute events.
+				 */
+				serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
+				serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
+				serializer.Serialize(xmlTextWriter, obj);
+				memoryStream = (MemoryStream) xmlTextWriter.BaseStream;
+				byte[] encodedBytes = memoryStream.ToArray();
+				return encoding.GetString(encodedBytes, 0, encodedBytes.Length);
+			}
+			catch(Exception e)
+			{
+				Tracer.Error(e.Message);
+				return null;
+			}
 		}
 
 		public static object Deserialize(Type objType, string text)
 		{
+			return Deserialize(objType, text, Encoding.UTF8);
+		}
+
+		public static object Deserialize(Type objType, string text, Encoding encoding)
+		{
 			if(null == text)
 			{
 				return null;
 			}
 
 			XmlSerializer serializer = new XmlSerializer(objType);
+			MemoryStream memoryStream = new MemoryStream(encoding.GetBytes(text));
+
 			/*
-			 * If the XML document has been altered with unknown 
-			 * nodes or attributes, handle them with the 
+			 * If the XML document has been altered with unknown
+			 * nodes or attributes, handle them with the
 			 * UnknownNode and UnknownAttribute events.
 			 */
 			serializer.UnknownNode += new XmlNodeEventHandler(serializer_UnknownNode);
 			serializer.UnknownAttribute += new XmlAttributeEventHandler(serializer_UnknownAttribute);
-			return serializer.Deserialize(new StringReader(text));
+			return serializer.Deserialize(memoryStream);
 		}
 
 		private static void serializer_UnknownNode(object sender, XmlNodeEventArgs e)
@@ -70,7 +94,5 @@
 		{
 			Tracer.ErrorFormat("Unknown attribute: {0}='{1}'", e.Attr.Name, e.Attr.Value);
 		}
-
 	}
 }
-



Mime
View raw message