Friday, November 18, 2011

Serialization & deserialization in C#

Hi guys, Been pretty busy lately with developing a web part that allows our data specialists to deploy workflow data feeds from test environments to (pre)production environments. Not going into details, but I would like to show you a simple example on how to (de)serialize objects so these can be passed on to WCF services / ASP.NET web services. Consider the following class:
    public class MetalBand
        private string _bandName;
        private int _intMembers;
        private string _genre;

        public MetalBand()

        public string BandName
            get { return _bandName; }
            set { _bandName = value; }

        public int NumberOfMembers
            get { return _intMembers; }
            set { _intMembers = value; }

        public string Genre
            get { return _genre; }
            set { _genre = value; }

Suppose you’ve got an instance of this object like so:
            MetalBand myBandObject = new MetalBand();
            myBandObject.BandName = "Skeletonwitch";
            myBandObject.Genre = "Blackened Thrash";
            myBandObject.NumberOfMembers = 5;
And you would need to pass this data on to a web service, you would need to serialize it first. This means that you convert an object state into a format which can be transported and/or stored. Here is how to:
            XmlSerializer serializer = new XmlSerializer(typeof(MetalBand));
            XmlWriterSettings writerSettings = new XmlWriterSettings();
            writerSettings.OmitXmlDeclaration = true;
            StringWriter stringWriter = new StringWriter();
            using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, writerSettings))
                serializer.Serialize(xmlWriter, myBandObject);

            XmlDocument doc = new XmlDocument();

            // here is your serialized object
The XmlSerializer class will do the actual magic of conversion, the MetalBand object type is passed on to the constructor of the class The instance of the XLMWriterSettings holds a supporting set of features on the XmlWriter object, which is created by the XmlWriter.Create. With the OmitXmlDeclaration property set to true, we state that we do not want to write an XML declaration in our document. We are then serializing the object instance into the XmlWriter stream with the chosen Xml declaration settings. Then we simply create a new instance of an XmlDocument and blast the xml stream right into it. As for deserialization, you can instantiate another object of XmlSerializer or use the same one created earlier. Then use the XmlNodeReader, which is also a stream reader, to stream that puppy into the Deserialize method.
            XmlSerializer xSerializer = new XmlSerializer(typeof(MetalBand));
            XmlNodeReader reader = new XmlNodeReader(doc);
            MetalBand mySecondBandObject = (MetalBand)xSerializer.Deserialize(reader);
That is all there is to it

No comments:

Post a Comment