Click here to Skip to main content
15,949,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the if statement below that is not working as I expect it to work. The xml document in objXMLInputDoc has 3 optional elements, AddPersonContactInformation, PersonTelephoneNumber and PersonEmailAddress
What I am trying to do is to check if any of the 3 elements exist and continue. If all are not present, the if statement will exit with an error.
It is working when all elements are not present. However when one element is present, the if statement still exists like. How do I fix the if statement so that, when there is one element present, it does not exit with an error?

This sample xml document is assigned to objXMLInputDoc. This is how if statement is accessing it and its elements.
Sample xml document
XML
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope>
	<soap:Body>
		<AddPersonContactInformation>
			<PersonAddress>
				<AddressUSStandard>
					<AddressAttention>Mr Smith</AddressAttention>
				</AddressUSStandard>
			</PersonAddress>
			<!--<PersonTelephoneNumber telephoneNumberUsageText="Home">
				<Telephone>698-478-4612</Telephone>
			</PersonTelephoneNumber>-->
			<!--<PersonEmailAddress>Testing@gmail.com</PersonEmailAddress>-->
		</AddPersonContactInformation>
	</soap:Body>
</soap:Envelope>


If statement not working
VB
If ((objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonAddress") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If


What I have tried:

VB
If ((objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonAddress") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) And (objXMLInputDoc.DocumentElement.SelectSingleNode("AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If
Posted
Updated 25-Apr-18 5:44am
Comments
Maciej Los 25-Apr-18 11:25am    
Are you sure? Seems that PersonAddress exists. So, it can't be null (nothing)!

1 solution

SelectSingleNode expects an XPath expression which starts at the current node.

Since AddPersonContactInformation is not a direct child of the root element, your XPath expression won't match anything.

Change your expressions to start with "//" to match the element anywhere in the document:
VB.NET
If ((objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonAddress") Is Nothing) AndAlso (objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonTelephoneNumber") Is Nothing) AndAlso (objXMLInputDoc.DocumentElement.SelectSingleNode("//AddPersonContactInformation/PersonEmailAddress") Is Nothing)) Then
'exit 
 Exit Sub
End If

XPath Tutorial[^]
XPath Syntax[^]

(NB: Your XML seems to be invalid - you're missing the soap namespace declaration.)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900