我有一个使用 XPathNavigator 迭代节点的应用程序。它工作正常。
但我想知道,如果我使用 LINQ to Xml....
我会得到什么好处(性能、可维护性)?
使用 XPath、LINQ to Xml 对性能有何影响?
我正在使用 C#.net、VS 2010 并且我的 .xml 是中等大小。
我有一个使用 XPathNavigator 迭代节点的应用程序。它工作正常。
但我想知道,如果我使用 LINQ to Xml....
我会得到什么好处(性能、可维护性)?
使用 XPath、LINQ to Xml 对性能有何影响?
我正在使用 C#.net、VS 2010 并且我的 .xml 是中等大小。
只是为了补充这里已经说明的内容,整体性能似乎取决于您对相关文档的实际操作。这是我基于一个简单的实验运行得出的结论,该运行比较了 XElement 和 XPathNavigator 之间的解析性能。
如果要选择节点,遍历这些节点并读取一些属性值:
另一方面,如果您还读取每个节点的子节点的值,它会变得有点有趣:
这些结论基于以下代码:
[Test]
public void CompareXPathNavigatorToXPathSelectElement()
{
var max = 100000;
Stopwatch watch = new Stopwatch();
watch.Start();
bool parseChildNodeValues = true;
ParseUsingXPathNavigatorSelect(max, watch, parseChildNodeValues);
ParseUsingXElementElements(watch, max, parseChildNodeValues);
ParseUsingXElementXPathSelect(watch, max, parseChildNodeValues);
ParseUsingXPathNavigatorFromXElement(watch, max, parseChildNodeValues);
}
private static void ParseUsingXPathNavigatorSelect(int max, Stopwatch watch, bool parseChildNodeValues)
{
var document = new XPathDocument(@"data\books.xml");
var navigator = document.CreateNavigator();
for (var i = 0; i < max; i++)
{
var books = navigator.Select("/catalog/book");
while (books.MoveNext())
{
var location = books.Current;
var book = new Book();
book.Id = location.GetAttribute("id", "");
if (!parseChildNodeValues) continue;
book.Title = location.SelectSingleNode("title").Value;
book.Genre = location.SelectSingleNode("genre").Value;
book.Price = location.SelectSingleNode("price").Value;
book.PublishDate = location.SelectSingleNode("publish_date").Value;
book.Author = location.SelectSingleNode("author").Value;
}
}
watch.Stop();
Console.WriteLine("Time using XPathNavigator.Select = " + watch.ElapsedMilliseconds);
}
private static void ParseUsingXElementElements(Stopwatch watch, int max, bool parseChildNodeValues)
{
watch.Restart();
var element = XElement.Load(@"data\books.xml");
for (var i = 0; i < max; i++)
{
var books = element.Elements("book");
foreach (var xElement in books)
{
var book = new Book();
book.Id = xElement.Attribute("id").Value;
if (!parseChildNodeValues) continue;
book.Title = xElement.Element("title").Value;
book.Genre = xElement.Element("genre").Value;
book.Price = xElement.Element("price").Value;
book.PublishDate = xElement.Element("publish_date").Value;
book.Author = xElement.Element("author").Value;
}
}
watch.Stop();
Console.WriteLine("Time using XElement.Elements = " + watch.ElapsedMilliseconds);
}
private static void ParseUsingXElementXPathSelect(Stopwatch watch, int max, bool parseChildNodeValues)
{
XElement element;
watch.Restart();
element = XElement.Load(@"data\books.xml");
for (var i = 0; i < max; i++)
{
var books = element.XPathSelectElements("book");
foreach (var xElement in books)
{
var book = new Book();
book.Id = xElement.Attribute("id").Value;
if (!parseChildNodeValues) continue;
book.Title = xElement.Element("title").Value;
book.Genre = xElement.Element("genre").Value;
book.Price = xElement.Element("price").Value;
book.PublishDate = xElement.Element("publish_date").Value;
book.Author = xElement.Element("author").Value;
}
}
watch.Stop();
Console.WriteLine("Time using XElement.XpathSelectElement = " + watch.ElapsedMilliseconds);
}
private static void ParseUsingXPathNavigatorFromXElement(Stopwatch watch, int max, bool parseChildNodeValues)
{
XElement element;
watch.Restart();
element = XElement.Load(@"data\books.xml");
for (var i = 0; i < max; i++)
{
// now we can use an XPath expression
var books = element.CreateNavigator().Select("book");
while (books.MoveNext())
{
var location = books.Current;
var book = new Book();
book.Id = location.GetAttribute("id", "");
if (!parseChildNodeValues) continue;
book.Title = location.SelectSingleNode("title").Value;
book.Genre = location.SelectSingleNode("genre").Value;
book.Price = location.SelectSingleNode("price").Value;
book.PublishDate = location.SelectSingleNode("publish_date").Value;
book.Author = location.SelectSingleNode("author").Value;
}
}
watch.Stop();
Console.WriteLine("Time using XElement.Createnavigator.Select = " + watch.ElapsedMilliseconds);
}
使用从这里下载的 books.xml
总体而言,XElement 解析 API(不包括 XPath 扩展)可以为您提供最佳性能,同时也更易于使用(如果您的文档有些扁平)。如果您有深层嵌套结构,则必须执行类似的操作
XElement.Element("book").Element("author").Element("firstname").SomethingElse()
那么 XElement.XPathSelectElement 可以提供性能和代码可维护性之间的最佳折衷。
好吧,XPathNavigator
通常会比Linq to XML
查询更快。但总有“但是”。
Linq to XML
肯定会使您的代码更具可读性和可维护性。阅读 linq 查询然后分析 XPath 更容易(至少对我而言)。此外 - 您将在编写查询时获得智能感知,这将有助于使您的代码正确。Linq to XML
如果您需要的话,还可以让您轻松修改数据。XPathNavigator
为您提供只读访问权限。
另一方面,如果你真的需要顶级性能,XPathNavigator
可能是要走的路。这仅取决于您当前的情况以及您要完成的工作。如果性能不是问题(XML 文件很小,您不会对该文件发出很多请求等等),您可以轻松地使用Linq to XML
. 否则贴近XPathNavigator
。