XMLDocument vs XDocument vs XmlReader vs LINQ to Xml [英] XMLDocument vs XDocument vs XmlReader vs LINQ to Xml

查看:55
本文介绍了XMLDocument vs XDocument vs XmlReader vs LINQ to Xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的项目编写一个应用程序,其中必须使用C#解析XML文档。所以它是这样的:



第一档:



I was writing an application for my Project in which XML documents have to be parsed using C#. So it was something like that:

1st file:

<name> Name of Student </name>
              <class> ClassofStudent </class>

依旧..



第二档:



< monday>

and so on..

2nd file :

<monday>

<period1>C++</period1>
                       <period2>Maths</period2>

等等onnn



和他们的5个文件更像他们。事情是,只有一个文件包含大量元素(它是一个整月的出勤记录),而其他都很简短。



我只是谷歌搜索所有内容,发现有不同的方法来解析C#中的XML,但有些方面存在高内存消耗或相对较低的速度等问题。



我无法做出决定的事情是我应该使用上述哪一项(XMLDocument或XDocument或XmlReader或LINQ to Xml)



或者我的目的是n不是那么高,所以我选择使用它并不重要吗?

我更喜欢Styling ==性能,因为我必须稍后设置它们(GUI)。哪一个将是更容易实现。

谢谢

and so onnn

and their are 5 files more like them.The thing is that only one of the file contains a huge number of elements ( its a kind of attendance record for whole month) while others are short.

I was just googling everything and found out there are different methods to parse XML in C#, but some face issues like high memory consumption or relative less speed.

The matter that i am unable to make decision is which of the above should i use (XMLDocument or XDocument or XmlReader or LINQ to Xml )

Or as my purpose will not be that much high , so it will not matter me what i choose to use?
I prefer Styling== performance , as i have to later style them also(GUI).And which one will be easier to implement too.
Thankyou

推荐答案

这一切都取决于许多不同的因素。最简单的是 XMLDocument XDocument (取决于你的任务,我也怀疑没有LINQ to XML的XDocument 会得到回报,所以考虑使用LINQ,但是 XmlReader / XmlWriter 可以获得最佳性能。请参阅我对这些方法的简短回顾:



It all depends on a number of different factors. The easiest would be XMLDocument or XDocument (depending on your tasks, also, I would doubt that XDocument without LINQ to XML would pay off, so consider using LINQ with that), but XmlReader/XmlWriter can get you top performance. Please see my short review of those approaches:

  1. 使用 System.Xml.XmlDocument 类。它实现了DOM接口;如果文档的大小不是太大,这种方式是最简单和最好的。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ]。
  2. 使用类 System.Xml.XmlTextWriter System.Xml.XmlTextReader ;这是最快的阅读方式,特别是你需要跳过一些数据。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx [ ^ ], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx [ ^ ]。
  3. 使用类系统.Xml.Linq.XDocument ;这是类似于 XmlDocument 的最合适的方式,支持LINQ to XML Programming。
    参见 http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx [ ^ ],http://msdn.microsoft.com/en-us/library/bb387063.aspx [ ^ ]。
  1. Use System.Xml.XmlDocument class. It implements DOM interface; this way is the easiest and good enough if the size if the document is not too big.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^].
  2. Use the classes System.Xml.XmlTextWriter and System.Xml.XmlTextReader; this is the fastest way of reading, especially is you need to skip some data.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmlwriter.aspx[^], http://msdn.microsoft.com/en-us/library/system.xml.xmlreader.aspx[^].
  3. Use the class System.Xml.Linq.XDocument; this is the most adequate way similar to that of XmlDocument, supporting LINQ to XML Programming.
    See http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx[^], http://msdn.microsoft.com/en-us/library/bb387063.aspx[^].





-SA


这篇关于XMLDocument vs XDocument vs XmlReader vs LINQ to Xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆