C# linq基于多个子元素从xml中选择父元素
如何选择C# linq基于多个子元素从xml中选择父元素,c#,xml,linq,c#-4.0,C#,Xml,Linq,C# 4.0,如何选择元素,其中包含任何指定值: <config> <client> <name>Localhost</name> <domains> <domain>localhost</domain> <domain>192.168.43.12</domain> </domains> <moduletype>t&l
元素,其中
包含任何指定值:
<config>
<client>
<name>Localhost</name>
<domains>
<domain>localhost</domain>
<domain>192.168.43.12</domain>
</domains>
<moduletype>t</moduletype>
<contactname>Home Manager</contactname>
<contactemail>a@a.com</contactemail>
<contactphone1>+133255111</contactphone1>
<contactphone2>+1332552</contactphone2>
</client>
<client>
<name>Client A</name>
<domains>
<domain>a.com</domain>
<domain>c.com</domain>
<domain>d.com</domain>
</domains>
<moduletype>t</moduletype>
<contactname>Client A</contactname>
<contactemail>info@c.com</contactemail>
<contactphone1>+12553254</contactphone1>
<contactphone2>+14403253</contactphone2>
</client>
</config>
但它会产生无效结果 您可以使用这样的查询:
XElement record = xmldoc.Element("config")
// from all client elements
.Elements("client")
// filter the one that has child element <domain> with value domain_name
.Where(x=>x.Descendants("domain").Any(v=>v.Value == domain_name))
// select only one or default
.SingleOrDefault();
XElement记录=xmldoc.Element(“配置”)
//从所有客户端元素
.要素(“客户”)
//筛选具有值为domain_name的子元素的元素
.其中(x=>x.substands(“域”).Any(v=>v.Value==域名))
//仅选择一个或多个默认值
.SingleOrDefault();
XElement record = xmldoc.Element("config")
// from all client elements
.Elements("client")
// filter the one that has child element <domain> with value domain_name
.Where(x=>x.Descendants("domain").Any(v=>v.Value == domain_name))
// select only one or default
.SingleOrDefault();