Click here to Skip to main content
15,881,856 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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..
<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.
XML
<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:
VB
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
Posted

1 solution

Try this:
C#
string orgCodes = string.Join(",", xDoc
			.Root
			.Descendants("userGroup")
			.Select(a=> a.Attribute("orgCode").Value));

Result:
111,ABC-123,QRX-567,BB1
 
Share this answer
 
Comments
Sascha Lefèvre 8-May-15 17:22pm    
+5
Maciej Los 9-May-15 2:46am    
Thank you, Sascha ;)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900