Apache.NMS.Util.XmlUtil.Serialize C# (CSharp) Method

Serialize() public static method

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.
public static Serialize ( object obj ) : string
obj object
return string
        public static string Serialize(object obj)
        {
            try
            {
                byte[] encodedBytes;

                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)
            {
                Tracer.ErrorFormat("Error serializing object: {0}", ex.Message);
                return null;
            }
        }

Usage Example

Beispiel #1
0
 /// <summary>
 /// Serialize the object as XML into the Text body of the message.
 /// Set the NMSType to the full name of the object type.
 /// </summary>
 /// <param name="message"></param>
 /// <param name="obj"></param>
 /// <returns></returns>
 internal static ITextMessage SerializeObjToMessage(ITextMessage message, object obj)
 {
     // Embed the type into the message
     message.NMSType = obj.GetType().FullName;
     message.Text    = XmlUtil.Serialize(obj);
     return(message);
 }
All Usage Examples Of Apache.NMS.Util.XmlUtil::Serialize