问题描述
我看你可以使用XmlDocument的或的XmlWriter创建XML文件。
I see you can create xml files using XmlDocument or XmlWriter.
有没有使用其中一种方法有什么好处?
Are there any benefits of using one method over another?
推荐答案
的XmlWriter
确实XML数据流为基础的写作。 的XmlDocument
构建XML数据在内存中的对象模型。
XmlWriter
does stream-based writing of XML data. XmlDocument
builds XML data as an object model in memory.
您使用的XmlWriter
当你需要生成XML文档,而无需使用内存成正比的文档的大小。您可以使用的XmlDocument
当你需要处理XML在内存中 - 当你分析一个现有的XML文档转换成一个对象模型,或者您正在创建中任意位置的元素,或者更新现有的文件,或者使用XPath在文档中进行搜索。 (实际上,对于后一种情况,你会使用的XPathDocument
,但在你的无论如何不能的使用的XmlWriter
,因为它是只写的。)
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.)
当然,这是可能使用的XmlDocument
编写XML数据流。但是,你实际使用的是的XmlWriter
要做到这一点,因为这是的XmlDocument
用内部。
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.
这篇关于XMLDOCUMENT VS的XmlWriter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!