使用Linq搜索一个表格的XML文件,可以做到吗?[英] Searching a tables XML filed using Linq, can it be done?

本文是小编为大家收集整理的关于使用Linq搜索一个表格的XML文件,可以做到吗?的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我有一个包含事务的数据库表.在其中一个字段中有一条 XML 消息,字段类型是"xml".在这个 XML 中有一个员工归档,我需要搜索这个字段.我想检索与运行时提供的员工编号匹配的所有行.可以用 Linq 做到这一点吗?

这里显示的是来自事务表中某一行的一些示例 XML.该字段称为"消息".我需要查看"employee"值,如果它与用户提供的值匹配,则返回该行.

<interface>
  <mac>1452345234</mac>
  <device>device1</device>
  <id>1234567</id>
  <terminal>
    <unit>1</unit>
    <trans>
      <event>A3</event>
      <employee>3333</employee>
      <time>2008-10-02T11:41:00.0000000+00:00</time>
    </trans>
  </terminal>
</interface>

推荐答案

是的,使用 LINQ 很容易实现:

var matchList = from t in transactions
                where XDocument.Load (new StringReader (t.Message))
                               .Descendants ("employee")
                               .Count (node => node.Value == employeeNr) > 0
                select t;

本文地址:https://www.itbaoku.cn/post/1557081.html

问题描述

I have a database table with transactions. In one of the fields there is an XML message, the field type is “xml”. In this XML there is an employee filed and it is this field that I need to search. I want to retrieve all the rows that match the employee number supplied at runtime. Is it possible to do this with Linq ?

Shown here is some example XML from one of the rows in the transaction table. The field is called “Message”. I need to look at the “employee” value and return the row if it match what was supplied by the user.

<interface>
  <mac>1452345234</mac>
  <device>device1</device>
  <id>1234567</id>
  <terminal>
    <unit>1</unit>
    <trans>
      <event>A3</event>
      <employee>3333</employee>
      <time>2008-10-02T11:41:00.0000000+00:00</time>
    </trans>
  </terminal>
</interface>

推荐答案

Yes, it is easily possible with LINQ:

var matchList = from t in transactions
                where XDocument.Load (new StringReader (t.Message))
                               .Descendants ("employee")
                               .Count (node => node.Value == employeeNr) > 0
                select t;