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.
There are four factory methods of this class being used extensively. Each method has their own uses as of requirement.
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.
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.
It can take an input Stream to write to and a character Encoding of the output:
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.
CreateTextWriter supports only:
-
UTF8 Encoding
-
UTF 16 little endian
-
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:
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.
This method allows us to pass boundary of MIME type also along with Stream to write.
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:
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.