Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XmlDocument vs XmlWriter

Tags:

c#

.net

xml

I see you can create xml files using XmlDocument or XmlWriter.

Are there any benefits of using one method over another?

like image 335
AJM Avatar asked Nov 02 '09 10:11

AJM


1 Answers

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.

like image 180
Robert Rossney Avatar answered Sep 20 '22 20:09

Robert Rossney