C# Linq to XML查询,根据附加条件选择特定子元素
下面是我当前的LINQ查询和示例XML。我要做的是从email addresses元素中选择主电子邮件地址到User.email属性中C# Linq to XML查询,根据附加条件选择特定子元素,c#,xml,linq,linq-to-xml,C#,Xml,Linq,Linq To Xml,下面是我当前的LINQ查询和示例XML。我要做的是从email addresses元素中选择主电子邮件地址到User.email属性中 下的类型元素 如果为true,则电子邮件地址元素设置为primary 可能有 下有多个元素 电子邮件地址,但只有一个将被标记为主 这里最简单的方法是什么 当前Linq查询(User.Email当前为空): XML示例: <?xml version="1.0" encoding="UTF-8"?> <response> <re
- 下的类型元素 如果为true,则电子邮件地址元素设置为primary李>
- 可能有 下有多个元素 电子邮件地址,但只有一个将被标记为主
<?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 & 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 & 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>
主要的,重要的
alice@domain.com
客户经理
用户
东部时间(美国和加拿大)
领域
爱丽丝
79629
2.
4.
3.
https://assets3.yammer.com/images/no_photo_small.gif
爱丽丝
https://www.yammer.com/domain.com/users/alice
积极的
https://www.yammer.com/api/v1/users/1089943
domain.com
1089943
主要的,重要的
bill@domain.com
办公室经理
用户
东部时间(美国和加拿大)
领域
比尔
79629
3.
1.
1.
https://assets3.yammer.com/images/no_photo_small.gif
账单
https://www.yammer.com/domain.com/users/bill
积极的
https://www.yammer.com/api/v1/users/1089920
domain.com
1089920
像这样:
response.Descendants("email-address")
.Single(a => a.Element("type").Value == "primary")
.Element("address").Value
请注意,如果没有一个匹配的元素,这将引发异常。如果不需要,请调用
FirstOrDefault
您可能希望使用Lambda表达式将
.Element(“联系人”).Element(“电子邮件地址”).Element(“电子邮件地址”)
替换为.Element(“电子邮件地址”):
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
};
是否总有一个元素的primary设置为true?是。始终有一个主元素设置为true。我不喜欢这种XML格式,我无法控制它;)感谢@Metro Surf的回答。它帮助解决了我的一个问题。
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
};