用于小型 XML 文件的 C# XmlDocument - 权重/性能
我想知道使用 XmlDocument 类处理小型 XML 文件的意义。 如果在加载XML文件的过程中,XmlDocument为所有XML元素及其关系创建一整套面向对象的结构,理论上对于小xml文件来说,会有轻微的性能下降和资源消耗,同时,我们可以清楚地看到和轻松的数据管理。
我读了很多关于 XML 文件处理的文章 - 通常人们建议使用轻量级解决方案,如 XmlTextReader 而不是 XmlDocument 来快速向前读取 - 即使对于小文件也是如此。是我错了还是 XmlDocument 没那么糟糕?
I wondering about sense of using XmlDocument class to processing a small XML files.
If during the XML file is loading, XmlDocument creates a whole set of object-oriented structure for all XML elements and their relationship, theoretically for small xml files, there is a slight performance decrease and resources consumption and in the same time, we have clear and easy data management.
I read many articles about the processing of XML files - usualy people suggest using lightweight solutions like XmlTextReader instead XmlDocument for fast forward reading - even for small files. Am I wrong or XmlDocument is not so bad?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在我看来,这是个人喜好。就我个人而言,我使用 Xmldocument 而不是 XmlTextReader,因为我发现它更容易使用。我现在已改用 LINQ to Xml,因为它使用 XDocument,可以更轻松地读取节点和元素
阅读 5 分钟的 LINQ to Xml 简介,因为它更易于使用和阅读 http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
In my opinion this is personal preference. Personally speaking I have used Xmldocument over XmlTextReader as I find it easier to work with. I have moved to use LINQ to Xml now as it uses XDocument which allows easier reading of nodes and elements
have a read of this 5 minute introduction to LINQ to Xml as it is a lot easier to use and to read http://www.hookedonlinq.com/LINQtoXML5MinuteOverview.ashx
您必须对其进行测量,但即使对于小文件,I/O 也将是主要因素。
您想如何使用该文件?这要重要得多。
我永远不会考虑使用 Xml(Text)Reader 来处理小文件。它唯一要优化的是内存使用。
回复评论:
对于 100kB,只需使用 XDocument 或旧的 XmlDocument
您可以使用 XElement.Descendants("TAG") 获取所有节点
You will have to measure it, but even for a small file the I/O will be the main factor.
And how do you want to use the file? That is far more important.
I would never consider an Xml(Text)Reader for small files. The only thing it would optimize is memory usage.
In reply to the comment:
For 100kB, just use XDocument or the older XmlDocument
You can get all your nodes with XElement.Descendants("TAG")