Click here to Skip to main content
15,898,222 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need help on how to create a variable to hold 3 phone objects (i.e objxmlHomePhone, objxmlCellPhone, objxmlWorkPhone).
My 3 objects are populated from xml document which I have added below.
The problem I have with my vb code is that when the xml document has no phone elements, my output shows empty elements. I do not want to show empty elements.

The following line of code is doing the update and responsible for showing the elements. objUpdateParty.Phones = {objxmlHomePhone, objxmlCellPhone, objxmlWorkPhone}

The For Each code is where I am trying to use a variable for the 3 phone objects. My hope was to be able to check if each phone object has a number. If it does not, then the code updating should ignore that phone object. This is actually where I need help.

Here is output by my code. You can see the first Phone element does not have Type or Number. I do not want this to appear.

XML
<?xml version="1.0"?>
<Phones>
	<Phone>
		<Type xsi:nil="true"/>
	</Phone>
	<Phone>
		<Number>223-789-6654</Number>
		<Type>Cell</Type>
	</Phone>
	<Phone>
		<Number>224-178-2222</Number>
		<Type>Work</Type>
	</Phone>
</Phones>


Here is my Xml document which in vb code is objXmlPartyNode There are two phone numbers in this xml document.

XML
<?xml version="1.0" encoding="UTF-8"?>
  <Phone Op="A" Current="true">
	<Type Op="A" Word="CELL">Cell</Type>
	<Number Op="A">223-789-6654</Number>
  </Phone>
  <Phone Op="A" Current="true">
	<Type Op="A" Word="WORK">Work</Type>
	<Number Op="A">224-178-2222</Number>
   </Phone>
<!--<Phone Op="A" Current="true">
	<Type Op="A" Word="HOME">Home</Type>
	<Number Op="A">225-789-6654</Number>
  </Phone>-->


What I have tried:

Here is code. I am reading an xml document to get the phone numbers

VB
Dim objxmlHomePhone As New MMGService.Phone()
Dim objxmlCellPhone As New MMGService.Phone()
Dim objxmlWorkPhone As New MMGService.Phone()
Dim objxmlPhones() As Object = {objxmlHomePhone, objxmlCellPhone, objxmlWorkPhone}
Dim objxmlPhoneNode As XmlNode

'Home phone
objxmlPhoneNode = objXmlPartyNode.SelectSingleNode("Phone[@Current='true' and Type/@Word='HOME']")
  If Not objxmlPhoneNode Is Nothing Then
        objxmlHomePhone.Type = MMGService.PhoneNumberTypes.Home
    If Not objxmlPhoneNode.SelectSingleNode("Number") Is Nothing Then
        objxmlHomePhone.Number = objxmlPhoneNode.SelectSingleNode("Number").InnerText
    End If
  End IF
'Work phone
objxmlPhoneNode = objXmlPartyNode.SelectSingleNode("Phone[@Current='true' and Type/@Word='WORK']")
  If Not objxmlPhoneNode Is Nothing Then
    objxmlWorkPhone.Type = MMGService.PhoneNumberTypes.Work
    If Not objxmlPhoneNode.SelectSingleNode("Number") Is Nothing Then
      objxmlWorkPhone.Number = objxmlPhoneNode.SelectSingleNode("Number").InnerText
    End If
 End If

'Cell phone
 objxmlPhoneNode = objXmlPartyNode.SelectSingleNode("Phone[@Current='true' and Type/@Word='CELL']")
   If Not objxmlPhoneNode Is Nothing Then
      objxmlCellPhone.Type = MMGService.PhoneNumberTypes.Cell
      If Not objxmlPhoneNode.SelectSingleNode("Number") Is Nothing Then
        objxmlCellPhone.Number = objxmlPhoneNode.SelectSingleNode("Number").InnerText
      End If
   End If
For Each objxmlPhone As Object In objxmlPhones
   If Not objxmlPhones Is Nothing Then
'This is where the update is done. When an object has no phone number, it is still updated i.e still shows up an an empty element. I do not want this to happen
    objUpdateParty.Phones = {objxmlHomePhone, objxmlCellPhone, objxmlWorkPhone}
   End If
Next
Posted
Updated 20-Nov-18 8:56am
v5

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