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 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
You can try using GroupBy()
and String.Join()
to get concatenated value of all attributes with the same name :
For Each attr In doc.Element("sslms").
Element("user").
Elements("userGroup").
Attributes().
GroupBy(Function(x) x.Name.ToString())
parseValues.Add("userGroup_" & attr.Key, String.Join(",", attr.Select(Function(x) x.Value)))
Next
Relevant part of the output :
userGroup_id --> XX1,ABC,XX2,XX5
userGroup_title --> MAIN GROUP,Test Group1,Test Group2,Test Group3
userGroup_orgCode --> 111,ABC-123,QRX-567,BB1
userGroup_type --> NORMAL,NORMAL,NORMAL,NORMAL
userGroup_fullPath --> /AAA/XX1/NANA,/ABC/,/AAA/XX2/,/AAA/XX5/BB1
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.