Click here to Skip to main content
15,882,837 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have xml document which I want to use a if statement to put conditions on so that the user does what the condition states.
Conditions:
1. If phoneInternationDirectDialID is present, it has to be 011 or nothing
2. If TelephoneCountryCodeID is present, it has to be 1 or nothing
3. TelephoneAreaCodeID must be included and has to be 3 digits - numeric
4. TelephoneExchangeID must be included and must have 3 numeric digits
5. TelephoneSubscriberID must be included and have 4 numeric digits.

How do I write a if statement to check and enforce these conditions. Thanks in advance.

My xml document
This xml document is in an object (vb.net) objXMLInputDoc
XML
<phoneNumber>
	<phoneInternationDirectDialID>612</phoneInternationDirectDialID><!--has to be 011 or nothing-->
	<phoneCountryCodeID>+93</phoneCountryCodeID><!--has to be 1 or nothing-->
	<phoneAreaCodeID>780</phoneAreaCodeID><!--has to be included and 3 digits-->
	<phoneExchangeID>347</phoneExchangeID><!--has to be included and 3 digits-->
	<phoneSubscriberID>8701</phoneSubscriberID><!--Has to be included and 4 digits-->
</phoneNumber>


What I have tried:

I want to use if statements like this but not sure how to finish it.
VB
'If phone number provided check for valid area code, TelephoneExchangeID and TelephoneSubscriberID
        'Check if TelephoneInternationDirectDialID and it must be 011
 If Not objXMLInputDoc.DocumentElement.SelectSingleNode("TelephoneNumber") Is Nothing Then
   'Add conditions here
 End If
Posted
Updated 1-May-18 9:35am
Comments
Jim Meadors 30-Apr-18 21:54pm    
It is not clear what you mean by "not sure how to finish it". Just add code for what you want to run inside the If statement.
Member 11403304 1-May-18 11:30am    
I mean how do I make sure the user only can enter 1 numeric digit for PhoneCountryCodeID or leave it blank. Make sure the must user must enter 3 numeric digits for PhoneAreasCode

1 solution

You're on the right track! SelectSingleNode[^] is proper method to get single XmlNode[^].

Note: you're using wrong name of node. There's no TelephoneNumber, but phoneNumber.

If you would like to make several comparisons, you have to firstly check if XmlNode is not Nothing and if it's not, then check out its Value[^].

VB
Dim n As XmlNode = objXMLInputDoc.DocumentElement.SelectSingleNode("phoneInternationDirectDialID")
If n IsNot Nothing Then
    If n.Value = "011" Then
        'here your logic
    End If
End If


In the same way, you have to do the rest of your comparisons...
 
Share this answer
 
Comments
Wendelius 2-May-18 22:55pm    
Good advice!
Maciej Los 3-May-18 13:33pm    
Thank you, Mika.

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