Hey All,
I understand there are better ways to do this but for this project I need to do it like this.
Trying to Populate a combo box with values stripped out of a XML file.
I have a module called mStrip, that handles the key components.
Now in the frmXMLStrip.vb the following code is used to load data ready to be populated into the combo box, the following code is used.
Now to populate the Combo Box, this piece of code is used.
Now as there are no button to initiate the code above, i write another piece of code that will automatically populate the combo box with the stripped values. Essentially all i need to do is make calls to the code that populates the combo boxes.
So that when i start the program, the sales person ID's from XML file should be loaded.
Is anyone able to tell me what is going wrong with my code? i'm not sure what parameters need to be passed into the to Call's in the last bit of code.
XML File included. http://www66.zippyshare.com/v/75509141/file.html
![]()
I understand there are better ways to do this but for this project I need to do it like this.
Trying to Populate a combo box with values stripped out of a XML file.
I have a module called mStrip, that handles the key components.
Code:
Public Function g_sDownload(ByVal sUrlA As String) As String
'Downloads the XML file with URL set in the form
' ---- Initial Step : Create a WebClient Instance
Dim oWebClient As New WebClient
Dim abWebContent() As Byte ' array used to download web resource
Dim sWebContent As String = Nothing ' string used to store the web resource (HTML or XML file)
Try
' --- Step 1: Call the DownLoadedData method
abWebContent = oWebClient.DownloadData(sUrlA)
' --- Step 2: Convert the byte array into a string
sWebContent = Encoding.ASCII.GetString(abWebContent)
Catch ex As Exception
MessageBox.Show("Detected Errors " & ex.Message)
Debug.Print("Error detected inside g_sDownload() in mStrip " & vbCrLf & "Detected Errors " & ex.Message)
End Try
Debug.Print("g_sDownload() - File: " & sUrlA & " Content uploaded successfully" & vbCrLf & sWebContent & vbCrLf & " ----------- " & vbCrLf)
Return sWebContent ' --- Return String with Text downloaded
End Function
Code:
Public Function g_alStripAllOneType(ByVal sFileTextA As String, ByVal sStartTagA As String, ByVal sEndTagA As String) As ArrayList
'This module handles the stripping of data, cohesively
' ---- Declare variables used for the stripping
Dim alLocalArray As New ArrayList ' --- used to store the stripped items that will be returned
Dim iStartTagPos As Integer = 0 ' --- Store start Position of Fixed Asset tag
Dim iEndTagPos As Integer = 0
Dim iStartSearching As Integer = 0
Dim iNoOfCharactersToStrip As Integer = 0
Dim iStartStrippingPosition As Integer = 0
Dim sStrippedValue As String = ""
Try
' ---- Step 4: Strip the value in between Start Tag- <xxx> and End Tag - </xxx>
' To strip we need to use the string methods: indexOf, SubString and Length
' ---- Step 4.1 Find Starting Position of First <xxx> startTag
iStartSearching = 0
iStartTagPos = sFileTextA.IndexOf(sStartTagA, iStartSearching)
' ---- Step 4.2 Use Do Loop to continue stripping the next start tag if found
Debug.Print("Inside g_alStripAllOneType() function: Just Before the loop starts ")
Debug.Print("The StartTag: " & sStartTagA & vbCrLf & " The EndTag: " & sEndTagA)
Debug.Print(" First searching Position in: iStartTagPos = " & iStartTagPos)
Do While iStartTagPos <> -1
' ---- Step 4.3 Strip the value in between the Start Tag <xxx> and End Tag </xxx>
iEndTagPos = sFileTextA.IndexOf(sEndTagA, iStartTagPos)
iStartStrippingPosition = iStartTagPos + sStartTagA.Length
iNoOfCharactersToStrip = iEndTagPos - iStartTagPos - sStartTagA.Length
sStrippedValue = sFileTextA.Substring(iStartStrippingPosition, iNoOfCharactersToStrip)
Debug.Print("iStartStrippingPosition: " & iStartStrippingPosition)
Debug.Print("iNoOfCharactersToStrip (should never be < 0): " & iNoOfCharactersToStrip)
' ---- Step 5: Add stripped value to the array list we are going to return
Debug.Print("sStrippedValue: " & sStrippedValue)
alLocalArray.Add(sStrippedValue)
' ---- Step 6: Search for next <xxx> start Tag in the sFileTextA variable
iStartSearching = iEndTagPos + 1 ' --- The last thing found in iEndTagPos is the starting search point of the next search
iStartTagPos = sFileTextA.IndexOf(sStartTagA, iStartSearching) ' --- Start the next search from the position of the last thing found
Debug.Print(" Next searching Position in: iStartTagPos = " & iStartTagPos)
Loop
Catch ex As Exception
MessageBox.Show("Detected Errors " & ex.Message)
Debug.Print("Error detected inside g_alStripAllOneType() in mStrip " & vbCrLf & "Detected Errors " & ex.Message)
End Try
Debug.Print(vbCrLf & " g_alStripAllOneType() - Finished executing ")
Return alLocalArray
End Function
Code:
Private Function loadDataForComboBox() As ArrayList
Dim alID As ArrayList = New ArrayList() ' --- used to store the stripped items that will be returned
' --- Note: The code you need here can be found in tute 3 ---------------------------------------------------
Try
' --- 1st. Download/upload the XML file with the data (sales person id) necessary to populate the combo box
Dim sURL As String
sURL = "file://" & Application.StartupPath & "\" & "salesperson_2013_S1.xml"
Dim sXMLText = g_sDownload(sURL) '- Assign the string returned by g_sDownload() 'makes a call to module posted above.
' ---- 2nd Strip the SalesPerson Id data (value from the Id tag) from the string variable with uploaded content
' call the module g_StripAllOneType passing the URL of file as well as the start and end tags that data needs to be stripped from.
Dim sStripID = g_alStripAllOneType(sXMLText, "<sale_person_id>", "</sale_person_id>")
Catch ex As Exception 'FileNotFoundException
MessageBox.Show("Detected Errors when stripping data from files " & ex.Message)
Debug.Print("Error detected inside loadDataForComboBox() in frmXML_2013_S1 " & vbCrLf & "Error: " & ex.Message)
End Try
Return alID
End Function
Code:
Private Sub populateComboBox(ByVal alValuesToShowA As ArrayList)
Dim iCount As Integer = 0
' --- The code below is hard coded for now
' --- You need to use a Loop to load data from an array to the ComboBox
For iCount = 0 To alValuesToShowA.Count - 1
cboSalesPerson.Items.Add(alValuesToShowA(iCount))
Next iCount
cboSalesPerson.Focus()
End Sub
So that when i start the program, the sales person ID's from XML file should be loaded.
Code:
Private Sub frmXML_2013_S1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
' --- 1st: You need to Call loadDataForComboBox() to load the data from SalesPerson XML file for the Combo Box
' --- For now we just hard code the array alIDsForCombo with three sales person ids
Call loadDataForComboBox()
' ---- 2nd: You need to call populateComboBox() to show the the IDs inside the Combo Box
Call populateComboBox()
End Sub
XML File included. http://www66.zippyshare.com/v/75509141/file.html
