How do you parse repeating XML sub row values in VB.net (2008) using XDocument, and Dictionary. In the code below I am able to read everything down to first row of userGroup..
Hide Copy Code
<userGroup id="XX1" title="MAIN GROUP" orgCode="111" type="NORMAL" fullPath="/AAA/XX1/NANA"/>
I get all the values for the FIRST row:

"XX1"
"MAIN GROUP"
"111"
"NORMAL"
"/AAA/XX1/NANA"

I don't know how to loop through the remaining userGroup rows to get the rest of the values.
I need ALL of the values from ALL of the rows starting with userGroup.

<userGroup id="XX1" title="MAIN GROUP" orgCode="111" type="NORMAL" fullPath="/AAA/XX1/NANA"/>
<userGroup id="ABC" title="Test Group1" orgCode="ABC-123" type="NORMAL" fullPath="/ABC/"/>
<userGroup id="XX2" title="Test Group2" orgCode="QRX-567" type="NORMAL" fullPath="/AAA/XX2/"/>
<userGroup id="XX5" title="Test Group3" orgCode="BB1" type="NORMAL" fullPath="/AAA/XX5/BB1"/>

There could be 1 to many userGroup rows.
My goals is to be able to read all values and store them into local variables.

Example: Reading all orgCode values from userGroup row(s) and storing them concatenated in a local string variable.

strLocal_VARIABLE_All_orgCodes = "111, ABC-123, QRX-567, BB1"

Sample Code:

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
    Dim result = ParseXML()
    For Each key1 In result.Keys
        Console.WriteLine(key1 & " --> " & result(key1))
    Next
End Sub

Function ParseXML() As Dictionary(Of String, String)

        Dim parseValues = New Dictionary(Of String, String)

        Dim doc As XDocument = <?xml version="1.0" encoding="UTF-8"?>
                               <sslms status="ok" time="0">
                                   <user username="123456" activated="true" userRole="END_USER" isCustomUser="false" selfReg="false" regDate="2015-01-17T20:08:30Z" siteLanguage="en-US" searchLanguage="en">
                                       <profileFieldValues>
                                           <fieldValue id="_sys_firstname">
                                               <value>John</value>
                                           </fieldValue>
                                           <fieldValue id="_sys_lastname">
                                               <value>Doe</value>
                                           </fieldValue>
                                           <fieldValue id="_sys_emailaddress">
                                               <value>JDoe@someplace.net</value>
                                           </fieldValue>
                                           <fieldValue id="_sys_display_first_name">
                                               <value>John</value>
                                           </fieldValue>
                                           <fieldValue id="_sys_display_last_name">
                                               <value>Doe</value>
                                           </fieldValue>
                                           <fieldValue id="_sys_location">
                                               <value/>
                                           </fieldValue>
                                           <fieldValue id="_sys_image_url">
                                               <value/>
                                           </fieldValue>
                                           <fieldValue id="ccnumber">
                                               <value>NO</value>
                                           </fieldValue>
                                       </profileFieldValues>
                                       <userGroup id="XX1" title="MAIN GROUP" orgCode="111" type="NORMAL" fullPath="/AAA/XX1/NANA"/>
                                       <userGroup id="ABC" title="Test Group1" orgCode="ABC-123" type="NORMAL" fullPath="/ABC/"/>
                                       <userGroup id="XX2" title="Test Group2" orgCode="QRX-567" type="NORMAL" fullPath="/AAA/XX2/"/>
                                       <userGroup id="XX5" title="Test Group3" orgCode="BB1" type="NORMAL" fullPath="/AAA/XX5/BB1"/>
                                   </user>
                               </sslms>

        For Each attr In doc.Element("sslms").Attributes()
            parseValues.Add("sslms_" & attr.Name.ToString, attr.Value)
        Next

        For Each attr In doc.Element("sslms").Element("user").Attributes()
            parseValues.Add("user_" & attr.Name.ToString, attr.Value)
        Next

        For Each ele In doc.Element("sslms").Element("user").Element("profileFieldValues").Elements("fieldValue")
            parseValues.Add(ele.Attributes.First.Value.ToString, ele.Element("value").Value)
        Next

        For Each attr In doc.Element("sslms").Element("user").Element("userGroup").Attributes()
            parseValues.Add("userGroup_" & attr.Name.ToString, attr.Value)
        Next

        Return parseValues

    End Function

Thanks

Hi

I'm not sure if this fully answers your question, but looking at the output that you used as an example, the following Linq to XML would be one way to do this:

Dim strLocal_VARIABLE_All_orgCodes As String = String.Empty

Dim doc As XDocument = XDocument.Load("c:\temp\Test.xml")
Dim elList = From el In doc...<userGroup>   'Get all elements for userGroup

For Each el As XElement In elList
    strLocal_VARIABLE_All_orgCodes &= "," & el.@orgCode 'Grab just the orgCode value for element
Next

'Remove initial comma from string.
strLocal_VARIABLE_All_orgCodes = strLocal_VARIABLE_All_orgCodes.Substring(1, strLocal_VARIABLE_All_orgCodes.Length - 1)

MessageBox.Show(strLocal_VARIABLE_All_orgCodes)

HTH

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.