0

How to select a <client> element where <domain> contains any specified value :

 <config>
  <client>
    <name>Localhost</name>
    <domains>
      <domain>localhost</domain>
      <domain>192.168.43.12</domain>
    </domains>
    <moduletype>t</moduletype>
    <contactname>Home Manager</contactname>
    <contactemail>[email protected]</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>[email protected]</contactemail>
    <contactphone1>+12553254</contactphone1>
    <contactphone2>+14403253</contactphone2>
  </client>
</config>

For eg : if "192.168.43.12" domain is passed, it should select client Localhost, if "c.com" domain is passed, it should select client Client A

I have tried :

string domain_name = "192.168.43.12";

XElement record = xmldoc.Element("config").Elements("client").Elements("domains").Where(x => (string)x.Element("domain") == domain_name).SingleOrDefault();

But it produces null result;

1 Answer 1

2

You can use such query:

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.