解析Xml的最有效方法。

[英]The most efficient way to parse Xml


The .Net framework now has (at least) four different methods of reading an Xml string. I've used each of XmlDocument, XmlReader, XPath and XElement, but which is the most efficient to use when coding or during execution? Is each designed for a different task, what are the pros and cons?

. net框架現在至少有四種讀取Xml字符串的方法。我已經使用了每一個XmlDocument、XmlReader、XPath和XElement,但是在編寫代碼或執行過程中,哪一個最有效?每一個都是為不同的任務設計的,有什么優點和缺點?


Update: Using a XmlReader appears to be the quickest way to read xml, which sound reasonable to me, but has it's limitations. I would like to know if there is any performance difference between XmlDocument and XLinq for accessing xml non-sequentially.

更新:使用XmlReader似乎是閱讀xml的最快方式,這對我來說聽起來很合理,但也有局限性。我想知道XmlDocument和XLinq之間是否存在性能差異,以便不按順序訪問xml。


Update: I found some posts comparing the different methods of loading an xml document. XmlReader is the fastest, there is insignificant difference between XmlDocument and LINQ to XML until you load a document with 10,000+ node where LINQ to XML comes out in front.

更新:我發現一些文章比較了加載xml文檔的不同方法。XmlReader是最快的,XmlDocument和LINQ到XML之間沒有什么區別,直到您加載一個具有10,000+節點的文檔,其中LINQ to XML出現在前面。

3 个解决方案

#1


12  

The three most common methods to read are:

閱讀最常見的三種方法是:

XmlDocument It reads the whole file in a tree structure that can then be accessed using XPath or by browsing all the nodes. It requires a lot of memory for very large file since the whole XML structure must be loaded in memory. Very good and simple to use for smaller files (less then a few megs).

XmlDocument以樹結構讀取整個文件,然后可以使用XPath或通過瀏覽所有節點來訪問該文件。對於非常大的文件,它需要大量的內存,因為整個XML結構必須在內存中加載。對於較小的文件(少於幾個megs),非常好和簡單。

XmlReader Fast, but also a real pain to use since it's sequential. If you ever need to go back, you can't, and XML structure are usually very prone to having disorganised orders. Also, if you read from a non ending stream of XML, this is probably the only way to go.

XmlReader速度很快,但由於它是連續的,使用起來也很麻煩。如果您需要返回,您不能,XML結構通常很容易出現無序的訂單。此外,如果您從無終止的XML流中讀取,這可能是惟一的方法。

XML serializers This basically does everything for you, you provide the root object of your model and it creates and read the XML for you. However, you have almost no control over the structure, and reading older versions of your object is a pain. So this won't work very well for persistance.

XML序列化器這基本上為您做了一切,您提供模型的根對象,它為您創建和讀取XML。然而,您幾乎無法控制結構,閱讀舊版本的對象是一件痛苦的事情。所以這對於堅持是行不通的。

XDocument and LINQ to XML As Daniel Straight pointed out. But I don't know it enough to comment. I invite anyone to edit the post and add the missing info.

正如Daniel直接指出的,XDocument和LINQ到XML。但我知道的還不夠多,無法發表評論。我邀請任何人編輯這篇文章並添加缺失的信息。


Now writing is another story. It's a pain to maintain a XmlDocument and XmlWriter is a breeze to use.

現在寫作又是另一回事了。維護一個XmlDocument和XmlWriter是一件很痛苦的事情。

I'd say, from my experience, that the best combo is to write using XmlWriter and read using XmlDocument.

根據我的經驗,最好的組合是使用XmlWriter編寫和使用XmlDocument讀取。

#2


5  

There's also XDocument and LINQ to XML, which I consider by far the most efficient when it comes to programmer time.

還有XDocument和LINQ to XML,我認為在程序員時間方面,這是最有效的。

#3


4  

The fastest one would be XmlTextReader. From MSDN:

最快的是XmlTextReader。從MSDN:

"Represents a reader that provides fast, non-cached, forward-only access to XML data."

表示提供快速、非緩存、只向前訪問XML數據的讀取器。

More here: XmlTextReader Class

更多:XmlTextReader類

Though it really depends on the problem to decide which "method" would be most apropriate for use. If you need to read an XML file only once (e.g.: reading and caching some global app-settings, etc.), then XmlTextReader is the winner. But remember, it is forward-only-reader. If you need to search/modify all-over the XML, then you should probably use XmlDocument Class.

雖然這確實取決於問題來決定哪一種“方法”最適合使用。如果您只需要讀取一次XML文件(例如:讀取和緩存一些全局應用程序設置等),那么XmlTextReader是贏家。但請記住,它是正向的——讀者。如果需要搜索/修改整個XML,那么應該使用XmlDocument類。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2009/03/03/55d0c653d63693541dac3f4f40e88925.html



 
  © 2014-2022 ITdaan.com 联系我们: