I'll put this out there to see if anyone has any thoughts. I apologize upfront for the long post.
I have an XML document that has a looping tag value that can loop up to 4 times. I'm parsing out values and displaying them in a VB Winform. The code I've been working on handles that with no problem. However, within each of those individual loops, there is a tag for CommunicationNumbers. Under that Tag, there are a number of number type tags. My plan is to parse those out and display them in a ListView control on the Winform. My problem is that within these various communication numbers (Primary,Beeper,Work,Home,etc.) any or all of those individual numbers can loop from 0 to infinity. This is where I'm having the problem. I have a ListView control for each loop of the main tag that I want populated with the numbers of the loop that I'm currently on. Instead, I'm getting both ListView controls populated with ALL of the numbers (from both main tags).
Here is a sample of the XML file i'm playing with:
<?xml version="1.0"?>
<Message xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" DatatypesVersion="20170715" TransportVersion="20170715" TransactionDomain="SCRIPT" TransactionVersion="20170715" StructuresVersion="20170715" ECLVersion="20170715">
<Header>
<To Qualifier="P">0101369</To>
<From Qualifier="D">9645424247001</From>
<MessageID>40f04b833a3c4a15b9117e808d386bbc</MessageID>
<SentTime>2019-03-20T13:48:44.9074605Z</SentTime>
<SenderSoftware>
<SenderSoftwareDeveloper>surescripts</SenderSoftwareDeveloper>
<SenderSoftwareProduct>auto</SenderSoftwareProduct>
<SenderSoftwareVersionRelease>0</SenderSoftwareVersionRelease>
</SenderSoftware>
<PrescriberOrderNumber>4987T6H43RKS8746RGSB3O86RS93S9879rq</PrescriberOrderNumber>
<PrescriberOrderGroup>
<OrderGroupNumber>DNSDOIUEWY937Y49879274H3SHQ98Y33XX2</OrderGroupNumber>
<ItemCountInOrderGroup>11</ItemCountInOrderGroup>
<TotalCountForOrderGroup>12</TotalCountForOrderGroup>
<OrderGroupReason>MultipleProductsPrescribed</OrderGroupReason>
</PrescriberOrderGroup>
</Header>
<Body>
<NewRx>
<Patient>
<HumanPatient>
<Name>
<LastName>Cliburn</LastName>
<FirstName>Gregory</FirstName>
</Name>
<Gender>M</Gender>
<DateOfBirth>
<Date>1963-06-24</Date>
</DateOfBirth>
<Address>
<AddressLine1>809 Hatcher St</AddressLine1>
<AddressLine2>Suite 1A</AddressLine2>
<City>Montgomery</City>
</Address>
<CommunicationNumbers>
<PrimaryTelephone>
<Number>3343334089</Number>
<Extension>4555</Extension>
<SupportsSMS>Y</SupportsSMS>
</PrimaryTelephone>
<PrimaryTelephone>
<Number>3346132151</Number>
<Extension>78965</Extension>
<SupportsSMS>Y</SupportsSMS>
</PrimaryTelephone>
<Beeper>
<Number>4445556666</Number>
<Extension>32321242</Extension>
<SupportsSMS>N</SupportsSMS>
</Beeper>
<Beeper>
<Number>5554446666</Number>
<Extension>32321242</Extension>
<SupportsSMS>N</SupportsSMS>
</Beeper>
<ElectronicMail>thisistheworldslongestmostxxxxxxxemailaddress@someoddexchangeservertblematic.com</ElectronicMail>
<Fax>
<Number>4521452147</Number>
<Extension>64646454</Extension>
<SupportsSMS>N</SupportsSMS>
</Fax>
<HomeTelephone>
<Number>2124451423</Number>
<Extension>64457457</Extension>
<SupportsSMS>N</SupportsSMS>
</HomeTelephone>
</CommunicationNumbers>
</HumanPatient>
</Patient>
<Patient>
<HumanPatient>
<Name>
<LastName>Langford</LastName>
<FirstName>Tyson!</FirstName>
</Name>
<Gender>M</Gender>
<DateOfBirth>
<Date>1967-12-29</Date>
</DateOfBirth>
<Address>
<AddressLine1>17 South Street</AddressLine1>
<AddressLine2>Apt 305</AddressLine2>
<City>Prattville</City>
</Address>
<CommunicationNumbers>
<PrimaryTelephone>
<Number>111222333</Number>
<Extension>45554745</Extension>
<SupportsSMS>Y</SupportsSMS>
</PrimaryTelephone>
<Beeper>
<Number>2123233652</Number>
<Extension>32321242</Extension>
<SupportsSMS>N</SupportsSMS>
</Beeper>
<Beeper>
<Number>5554446666</Number>
<Extension>32321242</Extension>
<SupportsSMS>N</SupportsSMS>
</Beeper>
<ElectronicMail>thisistheworldslongestmostxxxxxxxemailaddress@someoddexchangeservertblematic.com</ElectronicMail>
<Fax>
<Number>4521452147</Number>
<Extension>64646454</Extension>
<SupportsSMS>N</SupportsSMS>
</Fax>
</CommunicationNumbers>
</HumanPatient>
</Patient>
</NewRx>
</Body>
</Message>
Now, here is the code I'm working with. As a note, When I receive the XML file, I'm saving it in a SQL database in its entirety in an XML datatype field. I read that into a string from the database before parsing. My sample code has a textbox control that I'm copying the XML file into to test parsing. Here is the code I am trying to make work:
Imports System
Imports System.Net
Imports System.IO
Imports System.Xml
Imports Microsoft.VisualBasic.Strings
Imports System.Xml.Serialization
Imports System.Text
Imports System.Xml.Schema
Imports System.Xml.XmlWriter
Imports System.Object
Imports System.MarshalByRefObject
Imports System.Net.WebRequest
Imports System.Net.HttpWebRequest
Imports System.Threading.Thread
Imports Microsoft.VisualBasic
Imports System.Data
Imports System.Data.SqlClient
Imports System.Security.Cryptography
Imports System.Xml.Linq
Public Class Form1
Public strFullString As String
Public strLastName As String
Public strFirstName As String
Public strGender As String
Public strPrimaryNumber As String
Public strBeeperNumber As String
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim xmlString = TextBox1.Text
strFullString = xmlString
Dim doc As XElement = XElement.Parse(strFullString)
Dim sr As New System.IO.StringReader(strFullString)
Dim doc1 As New Xml.XmlDocument
'Dim doc As XDocument = XDocument.Parse(strFullString)
doc1.Load(sr)
Dim reader As New Xml.XmlNodeReader(doc1)
With ListView1
.View = View.Details
.FullRowSelect = True
.Columns.Add("Contact Type", 150)
.Columns.Add("", 300)
End With
Me.Controls.Add(ListView2)
With ListView2
.View = View.Details
.FullRowSelect = True
.Columns.Add("Contact Type", 150)
.Columns.Add("", 300)
End With
Me.Controls.Add(ListView2)
Dim query = From d In doc.<Body>.<NewRx>.<Patient>.<HumanPatient> _
Select New With { _
.LastName = d.<Name>.<LastName>.Value, _
.FirstName = d.<Name>.<FirstName>.Value, _
.Gender = d.<Gender>.Value _
}
Dim loopcounter As Integer
loopcounter = 0
For Each d In query
strLastName = d.LastName
strFirstName = d.FirstName
strGender = d.Gender
If loopcounter = 0 Then
txtFirstLoopLastName.Text = strLastName
'this is intended to get the looping phone numbers for the patient in question.
'each patient can have from 0 to infinity number of each specific number type
'i.e. Primary, beeper, fax, etc
'so I'm trying to get the numbers for the first Patient in the document
Dim queryNumbers = From a In doc.<Body>.<NewRx>.<Patient>.<HumanPatient>.<CommunicationNumbers>.<PrimaryTelephone> _
Select New With {.Number = a.<Number>.Value}
For Each a In queryNumbers
strPrimaryNumber = a.Number
Dim strDescription As String
If strPrimaryNumber > "" Then
strDescription = "Primary Number"
Dim lvi As New ListViewItem(strDescription)
lvi.SubItems.Add(strPrimaryNumber)
ListView1.Items.Add(lvi)
End If
Next
Dim queryBeeper = From a In doc.<Body>.<NewRx>.<Patient>.<HumanPatient>.<CommunicationNumbers>.<Beeper> _
Select New With {.Number = a.<Number>.Value}
For Each a In queryBeeper
strBeeperNumber = a.Number
Dim strDescription As String
If strBeeperNumber > "" Then
strDescription = "Beeper Number"
Dim lvi As New ListViewItem(strDescription)
lvi.SubItems.Add(strBeeperNumber)
ListView1.Items.Add(lvi)
End If
Next
End If
If loopcounter = 1 Then
txtSecondLoopLastName.Text = strLastName
'so I'm trying to get the numbers for the second Patient in the document
Dim queryNumbers = From a In doc.<Body>.<NewRx>.<Patient>.<HumanPatient>.<CommunicationNumbers>.<PrimaryTelephone> _
Select New With {.Number = a.<Number>.Value}
For Each a In queryNumbers
strPrimaryNumber = a.Number
Dim strDescription As String
If strPrimaryNumber > "" Then
strDescription = "Primary Number"
Dim lvi As New ListViewItem(strDescription)
lvi.SubItems.Add(strPrimaryNumber)
ListView2.Items.Add(lvi)
End If
Next
Dim queryBeeper = From a In doc.<Body>.<NewRx>.<Patient>.<HumanPatient>.<CommunicationNumbers>.<Beeper> _
Select New With {.Number = a.<Number>.Value}
For Each a In queryBeeper
strBeeperNumber = a.Number
Dim strDescription As String
If strBeeperNumber > "" Then
strDescription = "Beeper Number"
Dim lvi As New ListViewItem(strDescription)
lvi.SubItems.Add(strBeeperNumber)
ListView2.Items.Add(lvi)
End If
Next
End If
loopcounter = loopcounter + 1
Next
End Sub
End Class
Hopefully someone can catch what I'm doing wrong here. I know the main looping mechanism works because I'm getting the right last names in the right textbox controls on the Winform.
Thanks in advance!
Gregg