XMLWriter vs XMLDictionaryWriter

JRS picture JRS · Aug 30, 2010 · Viewed 9.9k times · Source

What's the difference between XMLWriter and XMLDictionaryWriter? In which cases is each one generally used?

Answer

Bronumski picture Bronumski · Sep 22, 2010

XmlWriter is an abstract class of which XmlDictionaryWriter is one of the classes that inherits from it and is itself an abstract class.

I am taking a stab in the dark that you want to use it with the DataContractSerializer or with de/serialization in general. The XmlDictionaryWriter is the base class used by WCF to do its de/serialization.

From that I would deduce that there must be some performance tuning in the XmlDictionaryWriter to make it more performant with WCF de/serialization tasks. In fact if you call the WriteObject(Stream, object) instead of WriteObject(XmlWriter, object) or WriteObject(XmlDictionaryWriter, object) methods it will create an XmlDictionaryWriter for you

public virtual void WriteObject(Stream stream, object graph)
{
    CheckNull(stream, "stream");
    XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(stream, Encoding.UTF8, false);
    this.WriteObject(writer, graph);
    writer.Flush();
}