You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@activemq.apache.org by jg...@apache.org on 2010/02/16 20:07:32 UTC
svn commit: r910651 - in /activemq/activemq-dotnet/Apache.NMS:
branches/1.2.x/src/main/csharp/Util/XmlUtils.cs
trunk/src/main/csharp/Util/XmlUtils.cs
Author: jgomes
Date: Tue Feb 16 19:07:32 2010
New Revision: 910651
URL: http://svn.apache.org/viewvc?rev=910651&view=rev
Log:
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)
Modified:
activemq/activemq-dotnet/Apache.NMS/branches/1.2.x/src/main/csharp/Util/XmlUtils.cs
activemq/activemq-dotnet/Apache.NMS/trunk/src/main/csharp/Util/XmlUtils.cs
Modified: activemq/activemq-dotnet/Apache.NMS/branches/1.2.x/src/main/csharp/Util/XmlUtils.cs
URL: http://svn.apache.org/viewvc/activemq/activemq-dotnet/Apache.NMS/branches/1.2.x/src/main/csharp/Util/XmlUtils.cs?rev=910651&r1=910650&r2=910651&view=diff
==============================================================================
--- activemq/activemq-dotnet/Apache.NMS/branches/1.2.x/src/main/csharp/Util/XmlUtils.cs (original)
+++ activemq/activemq-dotnet/Apache.NMS/branches/1.2.x/src/main/csharp/Util/XmlUtils.cs Tue Feb 16 19:07:32 2010
@@ -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)
{
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=910651&r1=910650&r2=910651&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 Tue Feb 16 19:07:32 2010
@@ -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)
{