c# - Linq-to-XML 查询以根据附加条件选择特定子元素

标签 c# xml linq linq-to-xml

我当前的 LINQ 查询和示例 XML 如下。我想要做的是从 email-addresses 元素中选择主要电子邮件地址到 User.Email 属性中。

  • type 元素下 email-address 元素在为真时设置为主要元素。
  • 有可能 下有多个元素 电子邮件地址,但只有一个将被标记为主要地址。

这里最简单的方法是什么?

当前的 Linq 查询(User.Email 当前为空):

var users = from response in xdoc.Descendants("response")
            where response.Element("id") != null
            select new User
                       {
                           Id = (string)response.Element("id"),
                           Name = (string)response.Element("full-name"),
                           Email = (string)response.Element("email-addresses"),
                           JobTitle = (string)response.Element("job-title"),
                           NetworkId = (string)response.Element("network-id"),
                           Type = (string)response.Element("type")
                       };

示例 XML:

<?xml version="1.0" encoding="UTF-8"?>
<response>
  <response>
    <contact>
      <phone-numbers/>
      <im>
        <provider></provider>
        <username></username>
      </im>
      <email-addresses>
        <email-address>
          <type>primary</type>
          <address>alice@domain.com</address>
        </email-address>
      </email-addresses>
    </contact>
    <job-title>Account Manager</job-title>
    <type>user</type>
    <expertise nil="true"></expertise>
    <summary nil="true"></summary>
    <kids-names nil="true"></kids-names>
    <location nil="true"></location>
    <guid nil="true"></guid>
    <timezone>Eastern Time (US &amp; Canada)</timezone>
    <network-name>Domain</network-name>
    <full-name>Alice</full-name>
    <network-id>79629</network-id>
    <stats>
      <followers>2</followers>
      <updates>4</updates>
      <following>3</following>
    </stats>
    <mugshot-url>
      https://assets3.yammer.com/images/no_photo_small.gif</mugshot-url>
      <previous-companies/>
      <birth-date></birth-date>
      <name>alice</name>
      <web-url>https://www.yammer.com/domain.com/users/alice</web-url>
      <interests nil="true"></interests>
      <state>active</state>
      <external-urls/>
      <url>https://www.yammer.com/api/v1/users/1089943</url>
      <network-domains>
        <network-domain>domain.com</network-domain>
      </network-domains>
      <id>1089943</id>
      <schools/>
      <hire-date nil="true"></hire-date>
      <significant-other nil="true"></significant-other>
    </response>
  <response>
    <contact>
      <phone-numbers/>
      <im>
        <provider></provider>
        <username></username>
      </im>
      <email-addresses>
        <email-address>
          <type>primary</type>
          <address>bill@domain.com</address>
        </email-address>
      </email-addresses>
    </contact>
    <job-title>Office Manager</job-title>
    <type>user</type>
    <expertise nil="true"></expertise>
    <summary nil="true"></summary>
    <kids-names nil="true"></kids-names>
    <location nil="true"></location>
    <guid nil="true"></guid>
    <timezone>Eastern Time (US &amp; Canada)</timezone>
    <network-name>Domain</network-name>
    <full-name>Bill</full-name>
    <network-id>79629</network-id>
    <stats>
      <followers>3</followers>
      <updates>1</updates>
      <following>1</following>
    </stats>
    <mugshot-url>
      https://assets3.yammer.com/images/no_photo_small.gif</mugshot-url>
      <previous-companies/>
      <birth-date></birth-date>
      <name>bill</name>
      <web-url>https://www.yammer.com/domain.com/users/bill</web-url>
      <interests nil="true"></interests>
      <state>active</state>
      <external-urls/>
      <url>https://www.yammer.com/api/v1/users/1089920</url>
      <network-domains>
        <network-domain>domain.com</network-domain>
      </network-domains>
      <id>1089920</id>
      <schools/>
      <hire-date nil="true"></hire-date>
      <significant-other nil="true"></significant-other>
    </response>
</response>

最佳答案

使用 Lambda 表达式:

var users = xdoc.Root.Elements( "response" )
    .Where( x => !string.IsNullOrEmpty( x.Element( "id" ).Value ) )
    .Select( x => new User
              {
                Id = x.Element( "id" ).Value,
                Name = x.Element( "full-name" ).Value,
                Email = x.Descendants( "email-address" )
                            .Where( y => y.Element( "type" ).Value == "primary" )
                            .First().Element( "address" ).Value,
                JobTitle = x.Element( "job-title" ).Value,
                NetworkId = x.Element( "network-id" ).Value,
                Type = x.Element( "type" ).Value,
              } );

查询表达式没有太大区别:

var users = from response in xdoc.Descendants( "response" )
            where response.Element( "id" ) != null
            select new User
            {
                Id = response.Element( "id" ).Value,
                Name = response.Element( "full-name" ).Value,
                Email = response.Descendants( "email-address" )
                            .Where( x => x.Element( "type" ).Value == "primary" )
                            .First().Element( "address" ).Value,
                JobTitle = response.Element( "job-title" ).Value,
                NetworkId = response.Element( "network-id" ).Value,
                Type = response.Element( "type" ).Value
            };

关于c# - Linq-to-XML 查询以根据附加条件选择特定子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2489433/

相关文章:

c# - IEnumerable<XElement> 和 foreach

c# - 日期时间解析问题

c# - 使用 OleDb 更新 Excel 工作表

c# - 大量数据的插入性能缓慢 (SQL Server/C#)

java - 使用 JRE 1.5 和 JDK 1.6 时 DocumentBuilder.parse 的区别

Android 评分 baar 无法正确显示

c# - Linq OrderBy 特定值不在分组内排序

linq - 人们为什么使用 linq to sql?

c# - 实现缺少新约束的通用接口(interface)

xml - XSLT 添加新标签并复制其下的一些现有元素