• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 472
  • Last Modified:

Dynamically Fill Array With Data From XML File

Experts, i am using the code below to get some information from an XML file. I need to put this information into an array dynamically. I will never know how many message there are and i will never know how many fields each message will have. Please help...

    Dim xdoc As DOMDocument60
    Set xdoc = New DOMDocument60
    xdoc.validateOnParse = False
   
    xdoc.Load ("C:\test.xml")

    Dim message As IXMLDOMElement
    Dim field As IXMLDOMElement
   
    For Each message In xdoc.selectNodes("/Messages/Message/Name")
       
        Debug.Print message.Text
       
        For Each field In xdoc.selectNodes("/Messages/Message[Name='" & message.Text & "']/Field[Type='Text']/Text")
       
            Debug.Print field.Text
           
        Next
       
    Next
0
Basicfarmer
Asked:
Basicfarmer
  • 3
  • 2
1 Solution
 
BasicfarmerAuthor Commented:
I didn't mention that i need the array to be two dimensional. One dimension for the messages and another dimension for the fields each message has.
0
 
anarki_jimbelCommented:
Do you really need to fill an array? I'd use a list of lists or dictionary of lists. This is much more flexible. Something like below:

            Dim dictionaryOfMessages As System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String))

            For Each Message As String In xdoc.selectNodes("/Messages/Message/Name")

                Debug.Print(Message.Text)


                Dim fieldList As New System.Collections.Generic.List(Of String)
                dictionaryOfMessages.Add(Message.Text, fieldList)
                For Each field In xdoc.selectNodes("/Messages/Message[Name='" & Message.Text & "']/Field[Type='Text']/Text")

                    Debug.Print(field.Text)
                    fieldList.Add(field.Text)

                Next

            Next

Open in new window


Later if you need you can transfer this data into a 2D array as you will know dimension sizes.
0
 
BasicfarmerAuthor Commented:
Can you be a little more specific on the use of the dictionary, i dont know how to use one. I need to capture the message names from the xml file and then capture all the fields for each message and store all of that information so i can look up the fields for a given message.
0
 
anarki_jimbelCommented:
Dictionary is one of most useful collections.

I already showed how to add data to a dictionary.

Below code shows how to retrieve data from dictionary:

        ' We have a dictionary:
        Dim dictionaryOfMessages As New System.Collections.Generic.Dictionary(Of String, System.Collections.Generic.List(Of String))
        ' here you fill dictionary from your xml
        ' ... see posted code

        ' Iterate through dictionary to see all entries:
        For Each messageText As String In dictionaryOfMessages.Keys
            System.Diagnostics.Debug.WriteLine("Message = " & messageText)
            System.Diagnostics.Debug.WriteLine("     Fields:")
            Dim fieldsForAMessage As System.Collections.Generic.List(Of String) = _
                    dictionaryOfMessages(messageText)
            For Each fielsText As String In fieldsForAMessage
                System.Diagnostics.Debug.WriteLine("     - " & fielsText)
            Next
        Next

Open in new window



You may also use a bit different approach. You may create a list of lists.
Say, you create a new class, e.g., MessageData, that have two fields: message text and a list of message fields.
For each message read from xml you create an instance of the class. Then you add each message data to the list:

dim messages as new List(of MessageData)

For ....
        messages .Add(MessageDataInstance)

Next
0
 
BasicfarmerAuthor Commented:
Great information and guidance. Thank you...
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now