问题
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