c# - 通过 xml 进行搜索的最快方法是什么

标签 c# xml search

假设我有一个 XML 文件,用作本地数据库,如下所示:

<root>
 <address>
  <firstName></firstName>
  <lastName></lastName>
  <phone></phone>
 </address>
</root>

我有几个问题:
1. 在 XML 中查找地址(例如 firstName 包含“er”)的最快方法是什么?
2. 是否可以不将整个 XML 文件加载到内存中?

附言我不是在寻找 XML 文件的替代品,理想情况下我需要一个不依赖于 XML 文件中地址计数的搜索。但我是现实主义者,在我看来这是不可能的。

更新: 我正在使用 .net 4
感谢您的建议,但这是比实际更科学的任务。我可能正在寻找比 linq 和 xmltextreader 更快的方法。

最佳答案

LINQ to Xml 工作得很好:

XDocument doc = XDocument.Load("myfile.xml");
var addresses = from address in doc.Root.Elements("address")
                where address.Element("firstName").Value.Contains("er")
                select address;

更新:尝试查看 StackOverflow 上的这个问题:Best way to search data in xml files? .

Marc Gravell 接受的答案使用 SQL 索引:

First: how big are the xml files? XmlDocument doesn't scale to "huge"... but can handle "large" OK.

Second: can you perhaps put the data into a regular database structure (perhaps SQL Server Express Edition), index it, and access via regular TSQL? That will usually out-perform an xpath search. Equally, if it is structured, SQL Server 2005 and above supports the xml data-type, which shreds data - this allows you to index and query xml data in the database without having the entire DOM in memory (it translates xpath into relational queries).

更新 2:另请阅读上一个问题中的另一个链接,该链接解释了 XML 的结构如何影响性能:http://www.15seconds.com/issue/010410.htm

关于c# - 通过 xml 进行搜索的最快方法是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5173062/

相关文章:

search - 如果在 Elasticsearch 中选择数组字段的另一个聚合选项,如何计算将添加的文档数

c# - 按等级排序字符串列表

c# - 软件设计与网络服务设计

java - XMLEventReader 和 XMLEventWriter 在同一个文件上?

python - 与 Boyer Moore 算法的重复字符串匹配关联

php - 使用 Codeigniter 在 InnoDB/MySQL 数据库中模拟全文搜索的最佳方法

c# - 无法将 User32.dll 导入 Visual Studio

c# - 在 C# 中使用矩阵标准化变换后的图像

Android:在 res/values 中组织字符串和字符串数组

xml - 以类似 SAX 的方式从磁盘二进制搜索 XML - 明智吗?可能的?