Check if xml node contains child child

Is there a way to check if the node I'm looking for using XPATHcontains trays or not?

I have an xml file in which a certain node sometimes has children, but for some time it hasn’t! How to check this, so I do not get too many NullReferenceExceptions !?

See an example below:

var text = xml.SelectSingleNode("/Document/Tests/Test").InnerText;

If the current xml file I'm working with has a Testnode inside it Tests, it works fine, but the Testsnode doesn’t contain anything in another XML file , and it’s just there for no apparent reason !?

Is there a command in XPATH for this? sort of!!

"/Document/Tests[NodeExist(Test)]

+5
source share
3 answers

/Document/Tests[Test] Tests, Test node .

/Document/Tests, element.GetElementsByTagName("Test").Count > 0, , Test node. , .

+4

xpath "// [node()]" , node. node, .

, node, Test "// []"

MSDN http://msdn.microsoft.com/en-us/library/ms256086.aspx

+1

Google :

/Document/Tests/not(*)
0

All Articles