XMLWriter vs XMLDictionaryWriter

£可爱£侵袭症+ 提交于 2019-12-18 19:08:13

问题


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


回答1:


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();
}



回答2:


XmlWriter is an abstract class used for writing XML documents in a forward-only manner. You use the static Create method to create concrete implementations of the class for use. This method of creating XML documents is useful when you need to quickly create arbitrary XML for whatever use.

XmlDictionaryWriter is an abstract class used for serialization and deserialization of objects to and from XML. From what I understand, it is used in WCF for serializing objects for moving across the wire. XmlDictionaryWriter can serialize using binary, text or MTOM formats. I would expect you would only use this class if you were specifically needing custom (de)serialization for use over WCF. See also here.

Hope that helps.



来源:https://stackoverflow.com/questions/3604123/xmlwriter-vs-xmldictionarywriter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!