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)
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 @@
/// </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)
{