- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有一个应用程序,我在其中使用 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);
}
使用从 here 下载的 books.xml
总的来说,如果您的文档有点扁平,看起来 XElement 解析 API(不包括 XPath 扩展)可为您提供最佳性能,同时也更易于使用。如果你有很深的嵌套结构,你必须做类似的事情
XElement.Element("book").Element("author").Element("firstname").SomethingElse()
那么 XElement.XPathSelectElement 可能会在性能和代码可维护性之间提供最佳折衷。
关于c# - 哪个是性能最好的 : XPathNavigator with XPath vs Linq to Xml with query?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8530416/
以下哪一个更好(EJB 3 JPA) //查询 一个)。 getEntityManager().createQuery("select o from User o"); //命名查询,其中 findA
也许其他人和我有同样的问题。我遇到了错误: Cannot execute queries while other unbuffered queries are active.Consider usin
我的代码 package com.tl666.elasticsearch.pojo; import lombok.AllArgsConstructor; import lombok.Data; imp
简短版:我想查询另一个查询的结果,以便选择更有限的结果集。但是,添加 where 子句会重写第一个查询而不是处理结果,因此我得不到我需要的答案。 详情:我有两个模型,支票和蜱虫。检查 has_many
我正在尝试使用 Doctrine 在 Symfony 框架中执行原始查询。 这是代码: class MessagesHandler { /** @var \Doctrine\Common\Pe
我正在运行以下两个语句: 首先是 A) 它做它需要做的事情并工作: SELECT itemColumn ,valueColumn ,label FROM rstCombinedChartD
我有一个脚本来查询数据库以获取订单信息,然后查询该查询以获取订单总数。代码看起来像这样。 SELECT oi.OrderQty, oi.ItemPrice FROM Ord
这个问题在这里已经有了答案: MySQL Insert query doesn't work with WHERE clause (31 个答案) 关闭 4 年前。 我正在从 php 更新数据库中的
在使用 Skygear JS SDK 时,查询是否返回数组? readDummy: function(){ const Test = skygear.Record.extend('
我想在一个表上运行 MySQL 查询,然后在该表上运行子查询。我有一个对象列表。每个对象都有一个主要版本和一个次要版本。对于一个对象,我试图找到该对象的“最后版本”:这意味着我想找到该对象的最大值(主
我正在尝试在 pod 中启动 prometheus,并在 k8s 中使用持久卷。 当我启动 pod 时,我看到: level=info ts=2021-09-12T13:58:13.120Z ca
基本上,我从 kube-prometheus-stack 安装了 Prometheues-Grafana使用提供的 helm chart repo prometheus-community # hel
是否可以根据另一个查询的结果在 TFS 2010 中创建新查询? 例如,一个(父)查询选择位于某个工作项下的所有工作项(假设 ID=5 的工作项)。现在我想创建其他查询,从第一个查询的结果中选择所有错
在 Delphi 中,每当我使用 TQuery 对数据库执行 SELECT 时,我都会在 Query.Open 后面加上 try..finally,并在finally 部分中使用 Query.Clos
我只是从一台服务器移动到另一台服务器。我的脚本在旧服务器上运行良好,但是我开始在新服务器上收到此错误: "Declaration of ezSQL_mysql::query() should be c
我想问一下有什么区别 for row in session.Query(Model1): pass 和 for row in session.Query(Model1).all():
如何使用注释通过spring-data-elasticsearch进行@Query(value =“{” query“:”“}”)的聚合? 最佳答案 您不能使用@Query注释来完成此操作,该注释的唯
我有一个对可变字符串执行 LIKE 条件的查询: 当变量包含一个包含单引号的单词时,返回一些结果,但不是全部: SELECT ID FROM MyQoQ
我有我的查询范围,它返回数百条记录。我需要在 Controller 中使用不同的过滤器查询这个集合。 我怎样才能做到这一点?可能吗? 查询范围: Client::join('transactions_
我有这样的数据库模式 用户 编号 初中生 文档 编号 标题 user_id(用户的外键) 模式(可以接受 PUBLIC 或 PRIVATE) 我想检索所有公开的文档和属于给定用户(矩阵)的所有文档 我
我是一名优秀的程序员,十分优秀!