XmlDocument vs XmlWriter XmlDocument vs XmlWriter xml xml

XmlDocument vs XmlWriter


XmlWriter does stream-based writing of XML data. XmlDocument builds XML data as an object model in memory.

You use XmlWriter when you need to produce XML documents without using memory proportional to the size of the document. You use XmlDocument when you need to process XML in memory - when you're parsing an existing XML document into an object model, or you're creating elements in arbitrary locations, or updating an existing document, or using XPath to search through a document. (Actually, for the latter case you'd use XPathDocument, but in any event you couldn't use an XmlWriter, since it's write-only.)

Of course it's possible to write XML data to a stream using an XmlDocument. But you're actually using an XmlWriter to do that, because that's what XmlDocument is using internally.


It will depend on your scenario. XmlDocument loads the whole document into memory, while XmlWriter uses a stream so it is better suited when working with large XML documents. On the other hand XmlDocument might be easier to use.


If you support .Net Framework 3.5 I would advise you to work with XDocument and XElement instead. It is a much simpler and friendlier API