You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by ta...@apache.org on 2017/03/06 17:01:03 UTC

[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)

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 <jg...@apache.org>
Authored: Tue Feb 16 19:07:32 2010 +0000
Committer: Jim Gomes <jg...@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)
 			{