我当前的 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")
};
示例 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 & 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>
最佳答案
使用 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/
我正在用Ruby编写一个简单的程序来检查域列表是否被占用。基本上它循环遍历列表,并使用以下函数进行检查。require'rubygems'require'whois'defcheck_domain(domain)c=Whois::Client.newc.query("google.com").available?end程序不断出错(即使我在google.com中进行硬编码),并打印以下消息。鉴于该程序非常简单,我已经没有什么想法了-有什么建议吗?/Library/Ruby/Gems/1.8/gems/whois-2.0.2/lib/whois/server/adapters/base.
我有一个对象has_many应呈现为xml的子对象。这不是问题。我的问题是我创建了一个Hash包含此数据,就像解析器需要它一样。但是rails自动将整个文件包含在.........我需要摆脱type="array"和我该如何处理?我没有在文档中找到任何内容。 最佳答案 我遇到了同样的问题;这是我的XML:我在用这个:entries.to_xml将散列数据转换为XML,但这会将条目的数据包装到中所以我修改了:entries.to_xml(root:"Contacts")但这仍然将转换后的XML包装在“联系人”中,将我的XML代码修改为
从给定URL下载文件并立即将其上传到AmazonS3的更直接的方法是什么(+将有关文件的一些信息保存到数据库中,例如名称、大小等)?现在,我既不使用Paperclip,也不使用Carrierwave。谢谢 最佳答案 简单明了:require'open-uri'require's3'amazon=S3::Service.new(access_key_id:'KEY',secret_access_key:'KEY')bucket=amazon.buckets.find('image_storage')url='http://www.ex
我正在使用Sequel构建一个愿望list系统。我有一个wishlists和itemstable和一个items_wishlists连接表(该名称是续集选择的名称)。items_wishlists表还有一个用于facebookid的额外列(因此我可以存储opengraph操作),这是一个NOTNULL列。我还有Wishlist和Item具有续集many_to_many关联的模型已建立。Wishlist类也有:selectmany_to_many关联的选项设置为select:[:items.*,:items_wishlists__facebook_action_id].有没有一种方法可以
我知道您通常应该在Rails中使用新建/创建和编辑/更新之间的链接,但我有一个情况需要其他东西。无论如何我可以实现同样的连接吗?我有一个模型表单,我希望它发布数据(类似于新View如何发布到创建操作)。这是我的表格prohibitedthisjobfrombeingsaved: 最佳答案 使用:url选项。=form_for@job,:url=>company_path,:html=>{:method=>:post/:put} 关于ruby-on-rails-rails:Howtomak
我试图在索引页中创建一个超链接,但它没有显示,也没有给出任何错误。这是我的index.html.erb代码。ListingarticlesTitleTextssss我检查了我的路线,我认为它们也没有问题。PrefixVerbURIPatternController#Actionwelcome_indexGET/welcome/index(.:format)welcome#indexarticlesGET/articles(.:format)articles#indexPOST/articles(.:format)articles#createnew_articleGET/article
我在pry中定义了一个函数:to_s,但我无法调用它。这个方法去哪里了,怎么调用?pry(main)>defto_spry(main)*'hello'pry(main)*endpry(main)>to_s=>"main"我的ruby版本是2.1.2看了一些答案和搜索后,我认为我得到了正确的答案:这个方法用在什么地方?在irb或pry中定义方法时,会转到Object.instance_methods[1]pry(main)>defto_s[1]pry(main)*'hello'[1]pry(main)*end=>:to_s[2]pry(main)>defhello[2]pry(main)
我有一个用户工厂。我希望默认情况下确认用户。但是鉴于unconfirmed特征,我不希望它们被确认。虽然我有一个基于实现细节而不是抽象的工作实现,但我想知道如何正确地做到这一点。factory:userdoafter(:create)do|user,evaluator|#unwantedimplementationdetailshereunlessFactoryGirl.factories[:user].defined_traits.map(&:name).include?(:unconfirmed)user.confirm!endendtrait:unconfirmeddoenden
我知道我可以指定某些字段来使用pluck查询数据库。ids=Item.where('due_at但是我想知道,是否有一种方法可以指定我想避免从数据库查询的某些字段。某种反拔?posts=Post.where(published:true).do_not_lookup(:enormous_field) 最佳答案 Model#attribute_names应该返回列/属性数组。您可以排除其中一些并传递给pluck或select方法。像这样:posts=Post.where(published:true).select(Post.attr
我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin