簡體   English   中英

讀取XML文件VB.NET時為空值

[英]Empty value when reading XML file VB.NET

我正在嘗試讀取一個簡單的xml文件值並將值保存到數組中。 問題在於它無法讀取該值,並且沒有邏輯錯誤。

    'data arrays
        Dim account_ids(0) As Integer
        Dim account_icons(0) As String
        Dim account_names(0) As String
        Dim account_paths(0) As String
    ' load accounts xml
    Dim xmlFilePath As String = "xmlAccountData.xml"
    If My.Computer.FileSystem.FileExists(xmlFilePath) = True Then
        Dim doc As XmlReader = New XmlTextReader(xmlFilePath)
        While (doc.Read())
            Dim type = doc.NodeType
            If (type = XmlNodeType.Element) Then
                If (doc.Name = "accountID") Then
                    Array.Resize(account_ids, account_ids.Length + 1)
                    account_ids(account_ids.Length - 1) = doc.ReadInnerXml.ToString()
                    MsgBox(doc.ReadInnerXml.ToString())
                End If
                If (doc.Name = "iconPath") Then
                    Array.Resize(account_icons, account_icons.Length + 1)
                    account_icons(account_icons.Length - 1) = doc.ReadInnerXml.ToString()
                    MsgBox(doc.ReadInnerXml.ToString())
                End If
                If (doc.Name = "accountName") Then
                    Array.Resize(account_names, account_names.Length + 1)
                    account_names(account_names.Length - 1) = doc.ReadInnerXml.ToString()
                    MsgBox(doc.ReadInnerXml.ToString())
                End If
                If (doc.Name = "accountPath") Then
                    Array.Resize(account_paths, account_paths.Length + 1)
                    account_paths(account_paths.Length - 1) = doc.ReadInnerXml.ToString()
                    MsgBox(doc.ReadInnerXml.ToString())
                End If
            End If
        End While
    End If

xml文件

<?xml version="1.0" standalone="yes"?>
<dsAccounts xmlns="http://tempuri.org/dsAccounts.xsd">
  <dt_Accounts>
    <accountID>0</accountID>
    <iconPath>path\bin\Debug\res\icon.png</iconPath>
    <accountName>asa</accountName>
    <accountPath>accounts\asa</accountPath>
  </dt_Accounts>
  <dt_Accounts>
    <accountID>1</accountID>
    <iconPath>path\bin\Debug\res\imageicon.png</iconPath>
    <accountName>drav</accountName>
    <accountPath>accounts\drav</accountPath>
  </dt_Accounts>
</dsAccounts>

問題讀取數據時,讀取每個值后都會彈出消息框。 但是msgbox不顯示任何內容。 陣列相同,不保存數據。 是空白

我有什么想念或需要做的事情才能讀取值。 其他項目中的相同代碼也可以正常工作。

    'data arrays
    Dim account_ids(0) As Integer
    Dim account_icons(0) As String
    Dim account_names(0) As String
    Dim account_paths(0) As String
    ' load accounts xml
    Dim xmlFilePath As String = "xmlAccountData.xml"
    If My.Computer.FileSystem.FileExists(xmlFilePath) = True Then
        Dim doc As XmlReader = New XmlTextReader(xmlFilePath)
        While (doc.Read())
            Dim type = doc.NodeType
            If (type = XmlNodeType.Element) Then
                If (doc.Name = "accountID") Then
                    Array.Resize(account_ids, account_ids.Length + 1)
                    account_ids(account_ids.Length - 1) = doc.ReadElementContentAsInt()
                    MsgBox(account_ids(account_ids.Length - 1).ToString())
                End If
                If (doc.Name = "iconPath") Then
                    Array.Resize(account_icons, account_icons.Length + 1)
                    account_icons(account_icons.Length - 1) = doc.ReadElementContentAsString()
                    MsgBox(account_icons(account_icons.Length - 1))
                End If
                If (doc.Name = "accountName") Then
                    Array.Resize(account_names, account_names.Length + 1)
                    account_names(account_names.Length - 1) = doc.ReadElementContentAsString()
                    MsgBox(account_names(account_names.Length - 1))
                End If
                If (doc.Name = "accountPath") Then
                    Array.Resize(account_paths, account_paths.Length + 1)
                    account_paths(account_paths.Length - 1) = doc.ReadElementContentAsString()
                    MsgBox(account_paths(account_paths.Length - 1))
                End If
            End If
        End While
    End If

暫無
暫無

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

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