Internal classes to understand WCF Message: XmlDictionaryWriter Class

XmlDictionaryWriter class is one of the most important classes to work with Message in WCF. This class is inherited form XmlWriter class. Essentially this takes as Stream as input parameter. This class mainly performs serialization and encoding of the stream to send as Message in WCF.

 

 

image

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

image

image

CreateDictionaryWriter method

It takes as input parameter 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.

 

image

If you see below code, we are creating a stream. Using stream we are creating a XmlWriter object and passing to XmlDictionaryWriter to create object of the same.


using System;
using System.ServiceModel.Channels;
using System.ServiceModel;
using System.Xml;
using System.Collections.Generic;
using System.IO;
namespace XML
{
    class Program
    {

        static void Main(string[] args)
        {


            MemoryStream stream = new MemoryStream();
            XmlWriter writer = XmlWriter.Create(stream);
            XmlDictionaryWriter dctWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
            Console.WriteLine(dctWriter.ToString());
            Console.ReadKey(true);


        }
    }

}


 

CreateTextWriter method

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

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

 

image

It can as input Stream to write to and character Encoding of the output

 

image

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

image

CreateTextWriter supports only,

  1. UTF8 Encoding
  2. UTF 16 little endian
  3. UTF16 big endian

 

We have been doing much of technical talk and I am sure you do not like it Smile so to please you let me show you, how we could use CreateTextWriter class.

In below code we are reading stream and writing 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
{
    class Program
    {

        static void Main(string[] args)
        {


            MemoryStream streamToWrite = new MemoryStream();

            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}", new StreamReader(streamToWrite).ReadToEnd());
            Console.ReadKey(true);


        }
}
}



On running you will get output as below,

image

If you would have noticed we used empty stream. So as a resultant we are getting 86 bytes written.

CreateMtomWriter method

This method is used to create MTOM-encoded XML.

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

image

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

image

We are just modifying the write method in 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
{
    class Program
    {

        static void Main(string[] args)
        {


            MemoryStream streamToWrite = new MemoryStream();


            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}", new StreamReader(streamToWrite).ReadToEnd());
            Console.ReadKey(true);


        }}}




Expected output you would get as below,

image

I hope this post was useful. I am taking a break here. In next post we will see CreateBinaryWriter method. Thanks for reading Smile

2 responses to “Internal classes to understand WCF Message: XmlDictionaryWriter Class”

  1. […] classes to understand WCF Message: XmlDictionary Class and Internal classes to understand WCF Message: XmlDictionaryWriter Class and Reading XML file through LINQ: few Tips (Dhananjay […]

  2. […] Internal classes to understand WCF Message: XmlDictionaryWriter Class […]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a website or blog at WordPress.com