Deserialising ebMS 3.0 Headers

The class below is my first attempt at being able to read the ebMS 3.0 headers in C#.  It deserialises the xml into the objects I have created from the xsd files (see previous post).  So you'll need the auto-generated C# classes to use this code.  I'll post an example useage in another blog.  So far I have tested it on all the examples extracted from the ebMS core spec and it seems to load the data OK.

using System;
using System.Collections.Generic;

using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel.Channels;

namespace ebMS3.Soap
{
    public class MessageDecoder
    {
        private string _xmlnamespace = null;
        public Message Message { getprivate set; } 

        public MessageDecoder(Stream stream)
        {
            _xmlnamespace = GetXmlNamespace(typeof(Messaging));
            Message = GetMessage(stream);  
        }

        public MessageDecoder(byte[] rawbytes)
        {
            _xmlnamespace = GetXmlNamespace(typeof(Messaging));
            MemoryStream ms = new MemoryStream(rawbytes); 
            Message = GetMessage(ms);
        }

        private string GetXmlNamespace(Type t)
        {
            XmlTypeAttribute[] attrs = (XmlTypeAttribute[])
                t.GetCustomAttributes(typeof(XmlTypeAttribute), true);
            if (attrs != null && attrs.Length > 0)
                return attrs[0].Namespace;
            else
                return "";
        }

        public UserMessage[] GetHeaderUserMessages()
        {
            return GetHeaderMessages<UserMessage>();
        }

        public SignalMessage[] GetHeaderSignalMessages()
        {
            return GetHeaderMessages<SignalMessage>();
        }

        private T[] GetHeaderMessages<T>() where T : class
        {
            if (Message == null || Message.Headers == nullreturn default(T[]);
            List<T> retval = new List<T>();
            XmlSerializer xs = new XmlSerializer(typeof(T), _xmlnamespace);

            XmlNode[] hdrs =
                Message.Headers.GetHeader<XmlNode[]>("Messaging", _xmlnamespace);
            foreach (XmlNode xn in hdrs)
            {
                if (xn.Name.EndsWith(typeof(T).Name))
                {
                    T um = xs.Deserialize(new XmlNodeReader(xn)) as T;
                    if (um != null) retval.Add(um);
                }
            }
            return retval.ToArray();
        }

        private Message GetMessage(Stream stream)
        {
            MessageVersion soap11 = MessageVersion.Soap11WSAddressingAugust2004;
            XmlReader xreader = XmlReader.Create(stream);
            return Message.CreateMessage(xreader, 5120, soap11); // 5Kb in headers
        }
    }
}

Turn ebMS 3.0 Headers into C# classes

Instructions for creating auto-generated C# classes for ebMS 3.0 headers
(we should end up with this file: ebms-header-3_0-200704_soap11_soap12.cs).

1) Download these xsd files to a folder.
ebms-header-3_0-200704.xsd:
http://docs.oasis-open.org/ebxml-msg/ebms/v3.0/core/ebms-header-3_0-200704.xsd
soap11.xsd:
http://schemas.xmlsoap.org/soap/envelope/
soap12.xsd:
http://www.w3.org/2003/05/soap-envelope/

2) Open a Visual Studio Command Prompt
Then change to the folder where the downloaded files are located

3) Run this command:
xsd /c /n:ebMS3.Soap ebms-header-3_0-200704.xsd soap11.xsd soap12.xsd