xmlノート走査

xmlファイルのノートを走査し、Dictionaryオブジェクトに保存します。MenuInfoはカスタムのエンティティクラス、三つプロパティーを持つ。id,name,value。

DictionaryオブジェクトのkeyはルートノートMenuInfoオブジェクトを保存して、valueは子ルートMenuInfoオブジェクトを保存します。

Public Function Dict() As Dictionary(Of MenuInfo, List(Of MenuInfo))

        'Dictionaryオブジェクト初期化
        Dim dictionary As New Dictionary(Of MenuInfo, List(Of MenuInfo))

        Dim list As New List(Of MenuInfo)
        'キーid
        Dim KeyId As String
        'キーname
        Dim KeyName As String
        'キーvalue
        Dim KeyValue As String
        '値id
        Dim ValueId As String
        '値name
        Dim ValueName As String
        '値value
        Dim Value As String
        'ルートノート初期化
        Dim father As XmlNode = xmlReader.Read(xml)

        For Each node1 As XmlNode In father.ChildNodes

            Dim MenuInfo_Key As New DH.SX.Framework.Entities.MenuInfo

            KeyId = node1.Attributes(0).Value
            KeyName = node1.Attributes(1).Value
            KeyValue = node1.Attributes(2).Value

           '取得した KeyId 、KeyName 、KeyValue をルートMenuInfoオブジェクトに注入します

    MenuInfo_Key.Id = KeyId
            MenuInfo_Key.Name = KeyName
            MenuInfo_Key.Value = KeyValue

            If Not node1.ChildNodes.Count = 0 Then

                For Each node2 As XmlNode In node1.ChildNodes

                    Dim MenuInfo_Value As New DH.SX.Framework.Entities.MenuInfo

                    ValueId = node2.Attributes(0).Value
                    ValueName = node2.Attributes(1).Value
                    Value = node2.Attributes(2).Value

                    MenuInfo_Value.Id = ValueId
                    MenuInfo_Value.Name = ValueName
                    MenuInfo_Value.Value = Value
                    list.Add(MenuInfo_Value)

                Next
                '子ノートが存在する場合、listでvalueを埋め込む
                dictionary.Add(MenuInfo_Key, list)
            Else
                '子ノートが存在しない場合、ヌルでvalueを埋め込む
                dictionary.Add(MenuInfo_Key, Nothing)
            End If

        Next

        Return dictionary
    End Function

上記のメソッドはただ二層のルートを走査する方法。

posted on 2012-03-09 21:05  吐个泡泡  阅读(86)  评论(0编辑  收藏  举报

导航