如何使用XDocument和Dictionary VB.net 2008从XML读取多个子行值 [英] How to read multiple sub row values from XML using XDocument and Dictionary VB.net 2008

查看:90
本文介绍了如何使用XDocument和Dictionary VB.net 2008从XML读取多个子行值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 XDocument 解析 VB.net (2008)中重复的XML子行值字典 的。在下面的代码中,我能够读到 userGroup 的第一行所有内容..

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">



我不知道如何遍历剩余的 userGroup 行以获取其余值。

我需要所有来自 userGroup 的行 ALL 的值。


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"/>



可能有1到多个 userGroup 行。

我的目标是能够读取所有值并将它们存储到局部变量中。



示例:从userGroup行读取所有orgCode值(s)并将它们连接在一个本地字符串变量中。



strLocal_VARIABLE_All_orgCodes =111,ABC-123,QRX-567,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

推荐答案

试试这个:

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



结果:


Result:

111,ABC-123,QRX-567,BB1


这篇关于如何使用XDocument和Dictionary VB.net 2008从XML读取多个子行值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆