ARTICLE

Internal Classes To Understand WCF Message: XmlDictionaryWriter Class

Posted by Dhananjay Kumar Articles | XML June 18, 2011
The XmlDictionaryWriter class is one of the most important classes for working with messages in WCF. This class is inherited from the XmlWriter class.
Reader Level:


The XmlDictionaryWriter class is one of the most important classes for working with messages in WCF. This class is inherited from the XmlWriter class. It essentially takes a Stream as an input parameter. The class mainly performs serialization and encoding of the stream to be sent as a message in WCF.

                                                         Untitled-23.gif

There are four factory methods of this class being used extensively. Each method has their own uses as of requirement.

Untitled-25.gif

Untitled-28.gif
CreateDictionaryWriter method

It takes as input parameter an object of XmlWriter. This method is not being used very extensively. This method could be very handy when we do not have any choice but XmlWriter object as input parameter.

Untitled-30.gif

In the following code we are creating a stream. Using the stream we create an XmlWriter object and pass it to a XmlDictionaryWriter to create an object from it.

using
System;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Xml;
using System.Collections.Generic;
using System.IO;
namespace XML
{
classProgram
    {     
 
    staticvoid Main(string[] args)
        { 
            MemoryStream stream = newMemoryStream();
            XmlWriter writer = XmlWriter.Create(stream);
            XmlDictionaryWriter dctWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
            Console.WriteLine(dctWriter.ToString());
            Console.ReadKey(true); 
        }       
    } 
}


CreateTextWritermethod

This method helps us to create text encoded XML. This is overloaded with three different types of input sets.

It can take only a Stream to write to. It writes output with the default encoding UTF8.

Untitled-32.gif

It can take an input Stream to write to and a character Encoding of the output:

Untitled-33.gif

It can take a Stream to write, character encoding of the output and a Boolean value. If the Boolean input is true then the Stream is closed by the writer once writing is done. Otherwise the Stream will not be closed by the writer.

Untitled-34.gif

CreateTextWriter supports only:

  1. UTF8 Encoding

  2. UTF 16 little endian

  3. UTF16 big endian

The explanations for the moment have been technical and I am sure you do not like it. So please let me show you how to use the CreateTextWriter class.

In the following code we are reading and writing a stream using CreateTextWriter.

using System;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Xml;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace XML
{
classProgram
    {     
 
staticvoid Main(string[] args)
        {
 
MemoryStream streamToWrite = newMemoryStream();
 
using (XmlDictionaryWriter wrt = XmlDictionaryWriter.CreateTextWriter(streamToWrite, Encoding.UTF8, false
            {
                wrt.WriteStartDocument();              
                wrt.WriteElementString("SongName", "urn:Mysong", "F:\\a.wmv");
                wrt.Flush();
            }
            Console.WriteLine("Number Of Bytes wriiten {0} ", streamToWrite.Position);
            Console.ReadKey(true);
            streamToWrite.Position = 0;
            Byte[] bytes = streamToWrite.ToArray();
            Console.WriteLine(bytes.Length);
            Console.ReadKey(true);
            Console.WriteLine(BitConverter.ToString(bytes));           
            Console.WriteLine("Data Read from stream : {0}", newStreamReader(streamToWrite).ReadToEnd());
            Console.ReadKey(true); 
        }
    }
}

On running you will get output as below:

Untitled-36.gif

Notice we used an empty stream. So as a result we are getting 86 bytes written.

CreateMtomWritermethod

This method is used to create MTOM-encoded XML.

We can pass Stream to write and other parameters like encoding.

Untitled-38.gif

This method allows us to pass boundary of MIME type also along with Stream to write.

Untitled-39.gif

We are just modifying the write method in the above code and you can see the change in output behavior as below:

using System;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Xml;
using System.Collections.Generic;
using System.IO;
using System.Text;
namespace XML
{
    classProgram
    {     
        staticvoid Main(string[] args)
        { 
            MemoryStream streamToWrite = newMemoryStream();           
 
            using (XmlDictionaryWriter wrt = XmlDictionaryWriter.CreateMtomWriter(streamToWrite,
            Encoding.UTF8,1000,
            "startinfo",
            "Boundary",
            "urn:stratUri",
            false,
            false))
            {
                wrt.WriteStartDocument();              
                wrt.WriteElementString("SongName", "urn:Mysong", "F:\\a.wmv");
                wrt.Flush(); 
            } 
            Console.WriteLine("Number Of Bytes wriiten {0} ", streamToWrite.Position); 
            streamToWrite.Position = 0;
            Byte[] bytes = streamToWrite.ToArray(); 
            Console.WriteLine(BitConverter.ToString(bytes));           
            Console.WriteLine("Data Read from stream : {0}", newStreamReader(streamToWrite).ReadToEnd());
            Console.ReadKey(true); 
            }}}

The output you would expect to get is as below:

Untitled-41.gif

I hope this post was useful. I am taking a break here. In the next article we will see the CreateBinaryWriter method.

Thanks for reading.

COMMENT USING
Employers - Post Free Jobs