Quantcast
Channel: VBForums
Viewing all articles
Browse latest Browse all 42457

VS 2010 Read in XML File and Put into array?

$
0
0

Hi Guys,

OK so I am enhancing my current project. Please see the below for a quick summary:

>Open ServiceUser XML file
>Read in the contents of the ServiceUser XML file into an array
>Filter any duplicates that may appear
>ReDim the size of the array

for example:

Code:

ServiceUsers.XML
<ServiceUser>1</ServiceUser>
<ServiceUserName>A</ServiceUser>
<ServiceUser>2</ServiceUser>
<ServiceUserName>B</ServiceUser>
<ServiceUser>3</ServiceUser>
<ServiceUserName>B</ServiceUser>
<ServiceUser>4</ServiceUser>
<ServiceUserName>C</ServiceUser>

the above is just a summary so as not to cause complication. As you can see, service user B appears a couple of times, but only really needs to be in the list once.

I also need to read in the Service User ID and the Service User Name, but have them as two seperate variables, so if the user chooses "Service User C" then it records the Service User as being "4".

Once I have gotten this into a combo box with Unique Values Only, I then need to (once the user has chosen a Service User) populate a second combo box with only values that apply to it, so I think something like this would be needed:

Code:

While (document.Read())
Read XML File WHERE ServiceUser = "B"
cbo.items.add(XMLString)
End While

Does any one have any suggestions on the above?

I have the below in place at this time:

Code:

    Private Sub ReadXMLFile1()
        Dim ServiceUserCount As Integer
        Dim PPServiceUserName As String

        ServiceUserCount = 0

        'check if file myxml.xml is existing
        If (IO.File.Exists("C:\ServiceUsers.xml")) Then

            'create a new xmltextreader object
            'this is the object that we will loop and will be used to read the xml file
            Dim document As XmlReader = New XmlTextReader("C:\ServiceUsers.xml")

            'loop through the xml file
            While (document.Read())

                Dim type = document.NodeType

                'if node type was element
                If (type = XmlNodeType.Element) Then

                    'if the loop found a <ServiceUserName> tag
                    If (document.Name = "ServiceUserName") Then

                        PPServiceUserName = (document.ReadInnerXml.ToString())
                        cboServiceUserName.Items.Add(PPServiceUserName) ''The Combo Box populates with the right values

                        ServiceUserCount = ServiceUserCount + 1

                    End If

'' The below is commented out, as when it is valid, it doesnt seem to allow the code above
                    'if the loop found a <ServiceUser> tag
                    ' If (document.Name = "ServiceUser") Then

                    'cboServiceUserID.Items.Add(document.ReadInnerXml.ToString())


                    ' End If
                End If

            End While

        Else

            MessageBox.Show("The filename you selected was not found.")
        End If
        MsgBox(ServiceUserCount) 'Returns39 correctly

        'Read more: http://forum.codecall.net/topic/69450-writing-and-reading-xml-files-vbnet-part-ii/#ixzz2C6UfNtZz 'Thanks to this site as this is where I took the examplar code from
    End Sub


Viewing all articles
Browse latest Browse all 42457

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>