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.
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 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.
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.
It can as input Stream to write to and character Encoding of the output
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.
CreateTextWriter supports only,
- UTF8 Encoding
- UTF 16 little endian
- UTF16 big endian
We have been doing much of technical talk and I am sure you do not like it 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,
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.
This method allows us to pass boundary of MIME type also along with Stream to write.
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,
I hope this post was useful. I am taking a break here. In next post we will see CreateBinaryWriter method. Thanks for reading
Leave a Reply