Linq to XML 获取 id 集合中带有 id 的项目
鉴于下面的 XML,我将如何获取在 coordinators.coordinator > 中拥有职位的员工列表?位置 ID?
<employee id="000001">
<username>Bernard</username>
<first_name>BERNARD</first_name>
<last_name>FISHER</last_name>
<business_phone>011111111111</business_phone>
<cell_phone>011111111112</cell_phone>
<hr_contact_name>PETER MANNING</hr_contact_name>
<contract_description>Permanent</contract_description>
<positions>
<position id="00000002" isPrimary="1">
<title>DEVELOPMENT MANAGER</title>
<department id="DV001">DEVELOPMENT</department>
<manager_position>00000002</manager_position>
<coordinators>
<coordinator position_id="00013662"/>
<coordinator position_id="00014488"/>
<coordinator position_id="00022675"/>
<coordinator position_id="00024364"/>
</coordinators>
</position>
</positions>
</employee>
<employee id="000002">
<!-- ... --->
</employee>
这是我到目前为止所拥有的:
//GET EMPLOYEE WITH CORORDINATORS AND SUBORINATES WITH POSITION ID
var q = from c in d.Elements().Elements().Elements("positions").Elements("position") where (string)(c.Attribute("id")) == "028782"
select new
{
PositionID = c.Parent.Parent.Attribute("id")
,Username = c.Parent.Parent.Element("username").Value
,FirstName = c.Parent.Parent.Element("first_name").Value
,LastName = c.Parent.Parent.Element("last_name").Value
,ContractDecription = c.Parent.Parent.Element("contract_description").Value
,Title = c.Element("title").Value
,Coordinators = (from coordinator
in d.Elements().Elements().Elements("positions").Elements("position")
join p in c on coordinator.Attribute("id").Value equals p.Attribute("id").Value
select new
{
PositionID = coordinator.Attribute("id")
,Username = coordinator.Parent.Parent.Element("username").Value
,FirstName = coordinator.Parent.Parent.Element("first_name").Value
,LastName = coordinator.Parent.Parent.Element("last_name").Value
,ContractDecription = coordinator.Parent.Parent.Element("contract_description").Value
,thenode = coordinator
})
,thenode = c
};
q.Dump();
Given the XML below how would I go about getting a list of employees who have a position in the coordinators.coordinator > position_ids?
<employee id="000001">
<username>Bernard</username>
<first_name>BERNARD</first_name>
<last_name>FISHER</last_name>
<business_phone>011111111111</business_phone>
<cell_phone>011111111112</cell_phone>
<hr_contact_name>PETER MANNING</hr_contact_name>
<contract_description>Permanent</contract_description>
<positions>
<position id="00000002" isPrimary="1">
<title>DEVELOPMENT MANAGER</title>
<department id="DV001">DEVELOPMENT</department>
<manager_position>00000002</manager_position>
<coordinators>
<coordinator position_id="00013662"/>
<coordinator position_id="00014488"/>
<coordinator position_id="00022675"/>
<coordinator position_id="00024364"/>
</coordinators>
</position>
</positions>
</employee>
<employee id="000002">
<!-- ... --->
</employee>
Here is what I have so far:
//GET EMPLOYEE WITH CORORDINATORS AND SUBORINATES WITH POSITION ID
var q = from c in d.Elements().Elements().Elements("positions").Elements("position") where (string)(c.Attribute("id")) == "028782"
select new
{
PositionID = c.Parent.Parent.Attribute("id")
,Username = c.Parent.Parent.Element("username").Value
,FirstName = c.Parent.Parent.Element("first_name").Value
,LastName = c.Parent.Parent.Element("last_name").Value
,ContractDecription = c.Parent.Parent.Element("contract_description").Value
,Title = c.Element("title").Value
,Coordinators = (from coordinator
in d.Elements().Elements().Elements("positions").Elements("position")
join p in c on coordinator.Attribute("id").Value equals p.Attribute("id").Value
select new
{
PositionID = coordinator.Attribute("id")
,Username = coordinator.Parent.Parent.Element("username").Value
,FirstName = coordinator.Parent.Parent.Element("first_name").Value
,LastName = coordinator.Parent.Parent.Element("last_name").Value
,ContractDecription = coordinator.Parent.Parent.Element("contract_description").Value
,thenode = coordinator
})
,thenode = c
};
q.Dump();
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你可以尝试这样的事情:
You can try something like that: