2

VB.net で解析しようとしている XML の醜いブロックがあります。基本的に、特定のノードを見つけて、その特定のノード内の、数ノード下に隠れているすべての情報を取得する必要があります。必要なノードを見つけて、その子ノードをノードリストに選択するために何かを構築できます。次に、そのノードリストをループして、そのノードリストから属性を取得できますが、選択したノードに基づいて新しいノードリストを作成する方法が見つからないようです。検索しても答えが見つからなかったので、根本的に愚かなことをしていると思います。私は基本的に<SixithLayer>、より深いすべての情報を取得して解析する必要がありますが、<SixithLayer>

私のコード:

Imports System
Imports System.IO
Imports System.Xml
Public Class Form1
    Sub ExampleXML()
        Dim my_XML_doc As XmlDocument = New XmlDocument
        Dim first_node_list As XmlNodeList
        Dim second_node_list As XmlNodeList
        my_XML_doc.Load("C:\temp\xmlExample.xml")
        first_node_list = my_XML_doc.SelectNodes("/FirstLayer/SecondLayer/ThirdLayer//FourthLayer[@Type='Type D']//FifthLayer/*")
        For Each node In first_node_list
            Dim grab_first_layer_info = node.Attributes.GetNamedItem("Name").Value
            second_node_list = node.SelectNodes("ImportantInfo")
            For Each node2 In second_node_list
                Dim grab_second_layer_info = node.Attributes.GetNamedItem("Name").Value
                'Some more for looping here to get all the attributes and and innerXML values hidden in here
                'unless there is a better way to quickly grab stuff that might be a variable
                'number of nodes deeper with varied names.
            Next
        Next
    End Sub
End Class

私のXML

<FirstLayer>
    <SecondLayer>
        <ThirdLayer>
            <FourthLayer Type="Type A" Name="FirstName"></FourthLayer>
            <FourthLayer Type="Type B" Name="SecondName"></FourthLayer>
            <FourthLayer Type="Type C" Name="ThirdName"></FourthLayer>
            <FourthLayer Type="Type D" Name="FourthName">
                <FifthLayer>
                    <SixthLayer Type="Step" Name="First">
                        <SomeJunk></SomeJunk>
                        <ImportantInfo Name="1stImportantStuff">
                            <StoreValue>
                                <Value>500</Value>
                            </StoreValue>
                            <MoreStuff Flavor="Purple" Look="Chocolate">
                                <Value>29</Value>
                            </MoreStuff>
                        </ImportantInfo>
                        <ImportantInfo Name="2ndImportantStuff">
                            <StoreValue>
                                <Value>TRUE</Value>
                            </StoreValue>
                        </ImportantInfo>
                        <ImportantInfo Name="3rdImportantStuff">
                            <StoreValue>
                                <Value>Cat</Value>
                            </StoreValue>
                        </ImportantInfo>
                    </SixthLayer>
                    <SixthLayer Type="Step" Name="Second">
                        <SomeJunk></SomeJunk>
                        <ImportantInfo Name="1stImportantStuff">
                            <StoreValue>
                                <Value>500</Value>
                            </StoreValue>
                        </ImportantInfo>
                        <ImportantInfo Name="2ndImportantStuff">
                            <StoreValue>
                                <Value>TRUE</Value>
                            </StoreValue>
                        </ImportantInfo>
                        <ImportantInfo Name="3rdImportantStuff">
                            <StoreValue>
                                <Value>Cat</Value>
                            </StoreValue>
                        </ImportantInfo>
                    </SixthLayer>
                </FifthLayer>
            </FourthLayer>
        </ThirdLayer>
    </SecondLayer>
</FirstLayer>

助けてくれてありがとう。

編集:以下のコメントに従って2番目のループが機能するように修正しました。どうやってそれを完全に見逃したのかわかりません。<ImportantStuff>より深くループする以外に、内外にあるすべての属性と内部テキスト情報を取得するためのより良い方法があるかどうかはまだ興味がありますが、これはかなり良い出発点です. 助けてくれてありがとう。

4

2 に答える 2

1

あなたがやっている方法が最も簡単な方法だと思います。ちょっと面倒ですが、独自のパーサーを作成するよりも高速です。

内側の for ループを別のサブに移動し、おそらくそのループの内側に別のサブを使用すると、コードの可読性が向上する可能性があります。

于 2013-09-10T03:27:14.063 に答える