I see you can create xml files using XmlDocument or XmlWriter.
Are there开发者_如何学C any benefits of using one method over another?
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
XmlDocument stores hole data in the memory, so it is slow when file is big(500 mb and more) but easy to use XmlWriter writes to stream directly
精彩评论