activemq-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tab...@apache.org
Subject [18/46] activemq-nms-api git commit: Always encode XML messages using UTF-8. It is the most platform neutral encoding, and is the de facto web standard. Fixes [AMQNET-230]. (See https://issues.apache.org/activemq/browse/AMQNET-230)
Date Mon, 06 Mar 2017 17:01:03 GMT
Always encode XML messages using UTF-8.  It is the most platform neutral encoding, and is the
de facto web standard.
Fixes [AMQNET-230]. (See https://issues.apache.org/activemq/browse/AMQNET-230)



Project: http://git-wip-us.apache.org/repos/asf/activemq-nms-api/repo
Commit: http://git-wip-us.apache.org/repos/asf/activemq-nms-api/commit/08b10d5f
Tree: http://git-wip-us.apache.org/repos/asf/activemq-nms-api/tree/08b10d5f
Diff: http://git-wip-us.apache.org/repos/asf/activemq-nms-api/diff/08b10d5f

Branch: refs/heads/1.2.x
Commit: 08b10d5fc0f59257dbb5ce3671b6dda039ec93c7
Parents: 1ec0293
Author: Jim Gomes <jgomes@apache.org>
Authored: Tue Feb 16 19:07:32 2010 +0000
Committer: Jim Gomes <jgomes@apache.org>
Committed: Tue Feb 16 19:07:32 2010 +0000

----------------------------------------------------------------------
 src/main/csharp/Util/XmlUtils.cs | 43 +++++++++++++++++++++++++++--------
 1 file changed, 33 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/activemq-nms-api/blob/08b10d5f/src/main/csharp/Util/XmlUtils.cs
----------------------------------------------------------------------
diff --git a/src/main/csharp/Util/XmlUtils.cs b/src/main/csharp/Util/XmlUtils.cs
index e250ac6..ea764d1 100644
--- a/src/main/csharp/Util/XmlUtils.cs
+++ b/src/main/csharp/Util/XmlUtils.cs
@@ -18,9 +18,9 @@
 using System;
 using System.IO;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Xml;
 using System.Xml.Serialization;
-using System.Text.RegularExpressions;
 
 namespace Apache.NMS.Util
 {
@@ -29,20 +29,43 @@ namespace Apache.NMS.Util
 	/// </summary>
 	public class XmlUtil
 	{
+		private static XmlWriterSettings xmlWriterSettings;
+
+		/// <summary>
+		/// Static class constructor.
+		/// </summary>
+		static XmlUtil()
+		{
+			xmlWriterSettings = new XmlWriterSettings();
+			xmlWriterSettings.Encoding = new UTF8Encoding(false, false);
+		}
+
+		/// <summary>
+		/// Serialize the object to XML format.  The XML encoding will be UTF-8.  A Byte Order
Mark (BOM)
+		/// will NOT be placed at the beginning of the string.
+		/// </summary>
+		/// <param name="obj"></param>
+		/// <returns></returns>
 		public static string Serialize(object obj)
 		{
 			try
 			{
-				StringBuilder outputStringBuilder = new StringBuilder();
-				XmlSerializer serializer = new XmlSerializer(obj.GetType());
-				XmlWriter xmlWriter = XmlWriter.Create(outputStringBuilder);
+				byte[] encodedBytes;
 
-				// Set the error handlers.
-				serializer.UnknownNode += serializer_UnknownNode;
-				serializer.UnknownElement += serializer_UnknownElement;
-				serializer.UnknownAttribute += serializer_UnknownAttribute;
-				serializer.Serialize(xmlWriter, obj);
-				return outputStringBuilder.ToString();
+				using(MemoryStream outputStream = new MemoryStream())
+				using(XmlWriter xmlWriter = XmlWriter.Create(outputStream, xmlWriterSettings))
+				{
+					XmlSerializer serializer = new XmlSerializer(obj.GetType());
+
+					// Set the error handlers.
+					serializer.UnknownNode += serializer_UnknownNode;
+					serializer.UnknownElement += serializer_UnknownElement;
+					serializer.UnknownAttribute += serializer_UnknownAttribute;
+					serializer.Serialize(xmlWriter, obj);
+					encodedBytes = outputStream.ToArray();
+				}
+
+				return xmlWriterSettings.Encoding.GetString(encodedBytes, 0, encodedBytes.Length);
 			}
 			catch(Exception ex)
 			{


Mime
View raw message