如何将linqxml列表数据显示到数据表

时间:2016-08-14 07:08:25

标签: linq c#-4.0 linq-to-xml

我有像这样的xml

<?xml version="1.0" encoding="utf-8"?>
<content>
  <field title="Year">
    <description>Numeric data</description>
    <comment>1234</comment>
  </field>
  <field title="mail">
    <description>Numeric data</description>
    <comment>ABCD</comment>
  </field>
<field title="Year">
    <description>AlphNumeric Data</description>
    <comment>ABCD1234</comment>
  </field>
</content>

我尝试在linq查询下面根据title属性提取数据。

var Data = XDocument.Load(Xmlpath).Root
          .Descendants("field")
          .Where(element => element.Attribute("title").Value.Contains("Year"))
          .Descendants()
          .Where(element => element.Name == "description" || element.Name == "comment")
          .Select(element => new { element.Name, element.Value }).ToList();

例如,我想显示标题为“year”的所有数据,输出应该显示在这样的数据表中

  Description                Comment
  numeric data               1234
  Alphanumeric Data          ABCD1234

但我不知道如何通过var数据将var数据转换为datatable或iterarte。有人可以帮助我吗?是否可以通过修改查询本身来显示linq查询的结果?

1 个答案:

答案 0 :(得分:0)

DataTable有一个读取XML的方法(ReadXml)。你尝试过使用它吗?

相关问题