簡體   English   中英

在VB.NET中解析復雜的XML

[英]Parsing complex XML in VB.NET

我試圖在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>

謝謝你的幫助。

編輯:對其進行了修復,以便使其根據下面的注釋在第二個循環中起作用。 不知道我是如何徹底錯過這個機會的。 仍然好奇是否有更好的方法來獲取<ImportantStuff>所有屬性和內部文本信息,以及循環得越來越深,但這是一個很好的開始。 謝謝您的幫助。

我相信您的做法是最簡單的方法。 即使很痛苦,它也比編寫自己的解析器要快。

如果將內部for循環移動到另一個子程序,然后在該循​​環的內部使用單獨的子程序,則可能會提高代碼的可讀性。

我正在尋找各種選擇,並選擇最佳和簡便的方法。

我搜索了LINQ to Xml,發現了有趣的文章,該文章討論解析XML。 您可以在LINQ查詢中應用where子句作為替代解決方案。

這是文章https://docs.microsoft.com/zh-cn/dotnet/visual-basic/programming-guide/concepts/linq/how-to-write-queries-on-xml-in-namespaces

希望這可以幫助。 謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM