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

Tar*_*ied 15 c# xml linq-to-xml

如何IncomingConfig使用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>
Run Code Online (Sandbox Code Playgroud)

L.B*_*L.B 25

bool b = xdocument.Descendants("IncomingConfig").Any();
Run Code Online (Sandbox Code Playgroud)

  • 或者,如果您只想检查`IncomingConfig`是否是根音符的子节点(而不是任何其他节点的后代),请使用`xdocument.Root.Element("IncomingConfig")!= null`. (7认同)