Convert XML to VB.NET Dictionary

你说的曾经没有我的故事 提交于 2019-12-10 08:32:02

问题


I'm trying to put sub-child values from XML into a dictionary collection using LINQ. I've done this with lists and custom collections which follow the same structure as the XML but am unable to search for specific values. If I know parentName, childName, and subChildName I want to be able to find subChildProperty1.value and subChildProperty2.value without iterating through the entire collection and each of the subsequent sub-collections, as I have to do with lists. This may not be the best implementation, and am open to suggestions, but still would like to figure out how to make this work. This would then allow me to have a dictionary item be:

key = "parentNameValue1.childNameValue1.subchildNameValue1.subChildProperty1"
value = 0

and I could just concatenate strings to form a specific key and search on that key to return a value.

XML:

<root>
    <parent>
        <parentName>parentNameValue1</parentName>
        <child>
            <childName>childNameValue1</childName>
            <subchild>
                <subchildName>subchildNameValue1</subchildName>
                <subChildProperty1>0</subChildProperty1>
                <subChildProperty2>5</subChildProperty2>
            </subchild>
            <subchild>
                <subchildName>subchildNameValue2</subchildName>
                <subChildProperty1>0</subChildProperty1>
                <subChildProperty2>10</subChildProperty2>
            </subchild>
        </child>
    </parent>
<root>

This question is somewhat similar to this question here but I could not get the code working in VB for my application.

I am new to SO (and VB) so I apologize if my etiquette is incorrect.


回答1:


With VB.NET's great XML support, this is quite easy to do:

Imports System.Xml.Linq

...

Sub Main()
    Dim xml = _
        <root>
            <parent>
                <parentName>parentNameValue1</parentName>
                <child>
                    <childName>childNameValue1</childName>
                    <subchild>
                        <subchildName>subchildNameValue1</subchildName>
                        <subChildProperty1>0</subChildProperty1>
                        <subChildProperty2>5</subChildProperty2>
                    </subchild>
                    <subchild>
                        <subchildName>subchildNameValue2</subchildName>
                        <subChildProperty1>0</subChildProperty1>
                        <subChildProperty2>10</subChildProperty2>
                    </subchild>
                </child>
            </parent>
        </root>

    ' Alternatively, load XML from a file
    ' Dim xml = XDocument.Load(fileName)

    Dim dict As New Dictionary(Of String, Integer)

    ' Extract the properties
    For Each parent In xml.<parent>
        Dim parentName = parent.<parentName>.Value
        For Each child In parent.<child>
            Dim childName = child.<childName>.Value
            For Each subchild In child.<subchild>
                Dim subchildName = subchild.<subchildName>.Value
                For Each prop In subchild.Elements.Where(Function(e) e.Name <> "subchildName")
                    dict.Add(String.Format("{0}.{1}.{2}.{3}", parentName, childName, subchildName, prop.Name), _
                             Integer.Parse(prop.Value))
                Next
            Next
        Next
    Next

    ' Print the values, to show that we've done a good job
    For Each kv In dict
        Console.WriteLine("{0}: {1}", kv.Key, kv.Value)
    Next
    Console.ReadLine()
End Sub

(Clearly, the code assumes Option Strict On and Option Infer On.)



来源:https://stackoverflow.com/questions/10804917/convert-xml-to-vb-net-dictionary

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!