Click here to Skip to main content
15,881,204 members
Articles / Programming Languages / C#

WS-Enumeration for WCF/WF

Rate me:
Please Sign up or sign in to vote.
4.86/5 (5 votes)
4 Feb 2011CPOL5 min read 25.8K   220   10  
In this article, I describe the design and implementation of a WS-Enumeration for WCF.
//*****************************************************************************
//    Description.....Message wrapper
//                                
//    Author..........Roman Kiss, rkiss@pathcom.com
//    Copyright � 2005 ATZ Consulting Inc. (see included license.rtf file)        
//                        
//    Date Created:    06/06/05
//
//    Date        Modified By     Description
//-----------------------------------------------------------------------------
//    06/06/05    Roman Kiss     Initial Revision
//    01/20/06    Roman Kiss     migrating to JanCTP (Go-Live)
//    03/03/06    Roman Kiss     migrating to FebCTP 
//    04/04/06    Roman Kiss     XmlToObject
//    07/07/06    Roman Kiss     migrating to JuneCTP 
//    11/11/06    Roman Kiss     RTM
//*****************************************************************************
//
#region References
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
using System.ServiceModel;
#endregion


namespace WSEnumeration
{
    public sealed class Convert
    {
        private Convert() { }
        public static T XmlToObject<T>(object resource)
        {
            if (resource is XmlElement && typeof(T) != typeof(object))
            {
                XmlElement element = resource as XmlElement;
                XmlSerializer xs = new XmlSerializer(typeof(T));
                using (MemoryStream ms = new MemoryStream())
                {
                    XmlTextWriter xtw = new XmlTextWriter(ms, Encoding.UTF8);
                    element.WriteTo(xtw);
                    xtw.Flush();
                    ms.Position = 0;
                    return (T)xs.Deserialize(ms);
                }
            }
            else
            {
                return (T)resource;
            }
        }

        /// <summary>
        /// Serialize object into the XmlElement
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static XmlElement ObjectToXml(object resource)
        {
            if (resource != null)
            {
                if (resource is XmlElement)
                {
                    return resource as XmlElement;
                }
                else if (resource is XmlNode)
                {
                    return resource as XmlElement;
                }
                else
                {
                    XmlDocument doc = new XmlDocument();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        XmlWriter xtw = XmlTextWriter.Create(ms, null);
                        XmlSerializer xs = new XmlSerializer(resource.GetType());
                        xs.Serialize(xtw, resource);
                        ms.Position = 0;
                        doc.Load(ms);
                    }
                    return doc.DocumentElement;
                }
            }
            return null;
        }

        /// <summary>
        /// Serialize object into the xml wrapper
        /// </summary>
        /// <param name="resource"></param>
        /// <returns></returns>
        public static XmlElement ObjectToXmlAnyElement(object resource)
        {
            if (resource != null)
            {
                if (resource is XmlElement)
                {
                    return resource as XmlElement;
                }
                else if (resource is XmlNode)
                {
                    return resource as XmlElement;
                }
                else
                {
                    XmlDocument doc = new XmlDocument();
                    using (MemoryStream ms = new MemoryStream())
                    {
                        XmlWriter xtw = XmlTextWriter.Create(ms, null);
                        xtw.WriteStartElement("root");
                        XmlSerializer xs = new XmlSerializer(resource.GetType());
                        xs.Serialize(xtw, resource);
                        xtw.WriteEndElement();
                        xtw.Flush();
                        ms.Position = 0;
                        doc.Load(ms);                                           
                    }
                    return doc.DocumentElement;
                }
            }
            return null;
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
I have Master degree in Particle Physics. During my last several years I work as software developer.

Primary Interests
- c#, c++, php, java.
- scientific programming

Comments and Discussions