18

How can I check if IncomingConfig element exists by use linq to xml?

<?xml version="1.0" encoding="utf-8"?>
<settings>
  <IncomingConfig>
    <ip>10.100.101.18</ip>
    <port>5060</port>
  </IncomingConfig>
  <Device>
    <username>tarek</username>
    <AgentName>tarek</AgentName>
    <password>ffff</password>
  </Device>
  <Device>
    <username>adf</username>
    <AgentName>adf</AgentName>
    <password>fadsf</password>
  </Device>
</settings>
KyleMit
  • 30,350
  • 66
  • 462
  • 664
Tarek Saied
  • 6,482
  • 20
  • 67
  • 111

1 Answers1

29
bool b = xdocument.Descendants("IncomingConfig").Any();
L.B
  • 114,136
  • 19
  • 178
  • 224
  • 9
    Alternatively, if you only want to check if `IncomingConfig` is a child of the root note (and not a descendant of any other node), use `xdocument.Root.Element("IncomingConfig") != null`. – Steve Guidi Aug 26 '12 at 16:00
  • @L.B my bad..I had mistaken the namespace as an element – Anirudha Aug 26 '12 at 16:03
  • Correction - "Element" should be plural .. so xdocument.Root.Elements("IncomingConfig") != null. Thanks ! – Leo Gurdian Aug 09 '23 at 04:41