歡迎您光臨本站 註冊首頁

XML數據讀取方式性能比較

←手機掃碼閱讀     火星人 @ 2014-03-09 , reply:0

幾個月來,疑被SOA,一直在和xml操作打交道,SQL差不多又忘光了.現在已經知道,至少有四種常用人XML數據操作方式(好像java差不多),不過還沒有實際比較過這些方式各有哪些特點或優劣.正好看到網上也沒有這方面的實驗,偶來總結一下.

測試開始先讀取XML源,用一個比較大的rss文件鏈接,複製到項目bin/debug目錄下.

Stream xmlStream = new MemoryStream(File.ReadAllBytes(path)); 一、XmlDocument 方式

代碼

static IList testXmlDocument()

{

var doc = new XmlDocument();

doc.Load(xmlStream);

var nodeList = doc.DocumentElement.ChildNodes;

var lstChannel = new List<Object>(nodeList.Count );

foreach (XmlNode node in nodeList)

{

var channel = new { Title = node.SelectSingleNode("title").

InnerText, Link = node.SelectSingleNode("link").

InnerText, Description = node.SelectSingleNode("description").

InnerText, Content = node.SelectSingleNode("content").

InnerText, PubDate = node.SelectSingleNode("pubDate").

InnerText, Author = node.SelectSingleNode("author").

InnerText, Category = node.SelectSingleNode("category").

InnerText };

lstChannel.Add(channel);

}

return lstChannel;

}

二、XPathNavigator 方式

代碼

static IList testXmlNavigator()

{

var doc = new XmlDocument();

doc.Load(xmlStream);

var nav = doc.CreateNavigator();

nav.MoveToRoot();

var nodeList = nav.Select("/channel/item");

var lstChannel = new List<Object>(nodeList.Count);

foreach (XPathNavigator node in nodeList)

{

var channel = new {

Title = node.SelectSingleNode("title").

Value, Link = node.SelectSingleNode("link").

Value, Description = node.SelectSingleNode("description").

Value, Content = node.SelectSingleNode("content").

Value, PubDate = node.SelectSingleNode("pubDate").

Value, Author = node.SelectSingleNode("author").

Value, Category = node.SelectSingleNode("category").

Value };

lstChannel.Add(channel);

}

return lstChannel;

}

三、XmlTextReader 方式

代碼

static List<Channel> testXmlReader()

{

var lstChannel = new List<Channel>();

var reader = XmlReader.Create(xmlStream);

while (reader.Read())

{

if

(reader.Name == "item" && reader.NodeType == XmlNodeType.Element)

{ var channel = new Channel();

lstChannel.Add(channel);

while (reader.Read())

{

if (reader.Name == "item") break;

if (reader.NodeType != XmlNodeType.Element) continue;

switch (reader.Name)

{

case "title": channel.Title = reader.ReadString();

break;

case "link": channel.Link = reader.ReadString();

break;

case "description": channel.Description = reader.ReadString();

break;

case "content": channel.Content = reader.ReadString();

break;

case "pubDate": channel.PubDate = reader.ReadString();

break;

case "author": channel.Author = reader.ReadString();

break;

case "category": channel.Category = reader.ReadString();

break;

default: break;

}}}}

return lstChannel;

}

四、Linq to XML 方式

代碼

static IList testXmlLinq()

{

var xd = XDocument.Load(xmlStream);

var list = from node in xd.Elements("channel").Descendants("item")

select new

{

Title = node.Element("title").

Value, Link = node.Element("link").

Value, Description = node.Element("description").

Value, Content = node.Element("content").

Value, PubDate = node.Element("pubDate").

Value, Author = node.Element("author").

Value, Category = node.Element("category").

Value };

return list.ToList();

測試結果:

XmlDocment 47ms

XPathNavigator 42ms

XmlTextReader 23ms

Xml Linq 28ms

小結一下自己的認識,XmlDocument的操作基本按W3C的DOM操作方式,不過要將全部節點解析成對象載入到內存中,往往造成很大浪費.所以微軟自己的編程規範也不推薦用它.這裡由於讀取了所有節點,可能因此性能和Navigator方式相差不大.在三種隨機讀取方式中,Xml Linq性能最高,只是方法名有點彆扭.XmlTextReader方式是所謂的SAX,只讀向前,無疑性能最高,不過實現上麻煩了不少,要比較精確的控制訪問邏輯,也無法用匿名類存儲數據.

.Net 3.5發布Xml Linq可以很好地取代前兩種方式,通常情況下,最好用它.只有個別場合,如果對性能要求極高,或者讀取Xml數據量太大不能一下子下載或讀取到內存中,那就只好痛苦委身於XmlTextReader了.


[火星人 ] XML數據讀取方式性能比較已經有714次圍觀

http://coctec.com/docs/java/show-post-59797.html