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