How to check if XElement has any child nodes?

三世轮回 提交于 2019-12-24 07:58:31

问题


I have the following condition,

if (myXElement.FirstNode.NodeType == XmlNodeType.CDATA)

This throws an exception if there is no FirstNode in myXElement, so I have to check first if there is any.

Note that I need to check for nodes not elements.


回答1:


var hasDescendants = myElement.Nodes().Any();



回答2:


Sorry for the VB but wouldn't this work

    If myXElement.Nodes.Count > 0 AndAlso myXElement.FirstNode.NodeType = Xml.XmlNodeType.CDATA Then

    End If


来源:https://stackoverflow.com/questions/37811047/how-to-check-if-xelement-has-any-child-nodes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!