Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/assembly/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# linq基于多个子元素从xml中选择父元素_C#_Xml_Linq_C# 4.0 - Fatal编程技术网

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();