开发者

Linq-to-XML query to select specific sub-element based on additional criteria

开发者 https://www.devze.com 2022-12-23 12:27 出处:网络
My current LINQ query and example XML are below. What I\'d like to do is select the primary email address from the email-addresses element into the User.Email property.

My current LINQ query and example XML are below. What I'd like to do is select the primary email address from the email-addresses element into the User.Email property.

  • The type element under the email-address element is set to primary when this is true.
  • There may be more than one element under the email-addresses but only one will be marked primary.

What is the simplest approach to take here?

Current Linq Query (User.Email is currently empty):

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")
                       };

Example 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>


Using Lambda Expression:

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,
              } );

The Query Expression isn't much different:

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
            };


Like this:

response.Descendants("email-address")
        .Single(a => a.Element("type").Value == "primary")
        .Element("address").Value

Note that this will throw an exception if there isn't exactly one matching element.
If you don't want that, call FirstOrDefault.

You might want to replace .Descendants("email-address") with .Element("contact").Element("email-addresses").Element("email-address").

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号