相关疑难解决方法(0)

Linq-to-XML查询,以根据其他条件选择特定的子元素

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

  • 类型的下元件 电子邮件地址元素被设置为主要时,这是真实的.
  • 电子邮件地址下可能有多个元素,但只有一个元素会被标记为主要元素 .

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

当前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")
                       };
Run Code Online (Sandbox Code Playgroud)

示例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 …
Run Code Online (Sandbox Code Playgroud)

c# xml linq linq-to-xml

5
推荐指数
1
解决办法
9056
查看次数

标签 统计

c# ×1

linq ×1

linq-to-xml ×1

xml ×1