如何检查Linq to XML中是否存在XML子元素

时间:2012-08-26 15:54:55

标签: c# xml linq-to-xml

如何使用linq to xml检查IncomingConfig元素是否存在?

<?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>

1 个答案:

答案 0 :(得分:24)

bool b = xdocument.Descendants("IncomingConfig").Any();