Skip to content
Permalink
Browse files
Change default XML serializing to UTF-8 format. Added overloaded func…
…tion for specifying different encoding format.
  • Loading branch information
Jim Gomes committed Oct 1, 2009
1 parent 775ccc5 commit 23af325751d6a15ef4e7bfabcd381d9015bc50d4
Showing 1 changed file with 38 additions and 16 deletions.
@@ -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 class XmlUtil
{
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 @@ private static void serializer_UnknownAttribute(object sender, XmlAttributeEvent
{
Tracer.ErrorFormat("Unknown attribute: {0}='{1}'", e.Attr.Name, e.Attr.Value);
}

}
}

0 comments on commit 23af325

Please sign in to comment.