簡體   English   中英

如何在 VB.NET 中讀取 XML 元素

[英]How to read XML elements in VB.NET

我有一個非常簡單的問題,但由於我是 XML 的新手,所以我遇到了一些問題。 我有這個 XML 文檔:

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout> 

我想要做的是將 LocX、LoxY、Width 和 Height 元素中的值讀入我相應的變量中。

這是我嘗試過的:

Dim XmlReader = New XmlNodeReader(xmlDoc)  
While XmlReader.Read  
    Select Case XmlReader.Name.ToString()  
        Case "Location"  
            If XmlReader.??  
        Case "Size"  
            If XmlReader.??
    End Select  
End While  

但是,我無法弄清楚如何訪問每個子節點。

如果您能夠使用 Linq to XML,則可以使用 VB 的XML 軸屬性

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.<Location>.<LocX>.Value)
Dim LocY = Integer.Parse(root.<Location>.<LocY>.Value)

而且root.<Location>.<LocY>.Value = CStr(120)工作。

下面介紹如何使用XmlDocument和 XPath 來完成此操作。 我相信其他人會很樂意自願提供一個使用XDocument和 LINQ 的示例。

Dim doc As New XmlDocument()
doc.LoadXml("...")
Dim locX As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocX").InnerText)
Dim locY As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Location/LocY").InnerText)
Dim width As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Width").InnerText)
Dim height As Integer = Integer.Parse(doc.SelectSingleNode("/FormLayout/Size/Height").InnerText)

此外,您可能想查看XmlSerializer類,看看您是否對它感興趣。該類將讀取 XML 文檔並使用它來填充新對象的屬性值。 您只需要創建一個類來模擬 XML 的結構,以便將其反序列化。

使用 LINQ-XML,

Dim root As XElement = XDocument.Load(fileName).Root

Dim LocX = Integer.Parse(root.Element("Location").Element("LocX").Value)
Dim LocY = Integer.Parse(root.Element("Location").Element("LocY").Value)

每個 XML 文檔可以在最頂部位置有一個聲明節點。 聲明節點提供有關 xml 文檔的信息:

<?xml version="1.0" encoding="utf-8"?> 

在聲明節點之后的 xml 文檔中必須存在根節點。 在你的例子中,根節點被命名為“Form_Layout”。

<Form_Layout>

System.Xml 命名空間中可用的類對象可用於讀取和操作數據,然后將數據保存到 xml 文件。 您可以使用文件流對象讀取 xml 文件的全部內容。 您可以修改節點,然后將其保存到新文件或覆蓋現有文件。 以下代碼可用於將 xml 文件加載到 XmlDataDocument 對象中。

    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing

您可以使用 XmlDataDocument 對象的 ChildNodes 屬性來讀取 xml 文檔的聲明節點和根節點。 您還可以調用每個節點的 ChildNodes 屬性以獲取其子節點作為回報。 您可以遍歷 XmlNodeList 對象中的項目以訪問各個節點。 使用您的 xml 文件的整個代碼如下。

Private Sub TestReadingAndSavingXml()
    Dim xmlDoc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument
    Dim filepath As String = "C:\Users\mraso\Documents\location.xml"
    Dim loadedIn As Boolean = False, readbln As Boolean = False
    Dim fstream As System.IO.FileStream = New System.IO.FileStream(filepath, System.IO.FileMode.Open, System.IO.FileAccess.Read)
    If fstream IsNot Nothing Then
        xmlDoc.Load(fstream)
        loadedIn = True
    End If
    fstream.Close()
    fstream.Dispose()
    fstream = Nothing
    Dim ndList1 As System.Xml.XmlNodeList = xmlDoc.ChildNodes
    If ndList1 IsNot Nothing Then
        Dim cnt As Integer = ndList1.Count
        For Each nd1 As System.Xml.XmlNode In ndList1
            If nd1.Name = "Form_Layout" Then
                Dim nd2 As System.Xml.XmlNode = GetChildNode(nd1, "Location")
                If nd2 IsNot Nothing Then
                    Dim nd3 As System.Xml.XmlNode = GetChildNode(nd2, "LocX")
                    If nd3 IsNot Nothing Then
                        Dim LocX_value As Integer = nd3.InnerText
                        Dim s = LocX_value
                    End If
                End If
            End If
        Next
    End If
    'Save data to a new xml file
    Dim outputXml As String = "C:\Users\mraso\Documents\location2.xml"
    xmlDoc.Save(outputXml)
End Sub

Function GetChildNode(ByRef node As System.Xml.XmlNode,
                      ByVal nodeName As String) As System.Xml.XmlNode
    If node IsNot Nothing Then
        Dim ndList1 As System.Xml.XmlNodeList = node.ChildNodes
        If ndList1 IsNot Nothing Then
            For Each nd1 As System.Xml.XmlNode In ndList1
                If nd1.Name = nodeName Then
                    Return nd1
                End If
            Next
        End If
    End If
    Return Nothing
End Function

上面的代碼將讀取以下 xml 文件中節點“LocX”中的值 100。

<?xml version="1.0" encoding="utf-8"?>  
<Form_Layout>
  <Location>
    <LocX>100</LocX>
    <LocY>100</LocY>  
  </Location>  
  <Size>  
    <Width>300</Width>  
    <Height>300</Height>  
  </Size>  
</Form_Layout>

暫無
暫無

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

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