幾個月來,疑被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了。