简体   繁体   中英

Empty value when reading XML file VB.NET

I'm trying to read a simple xml file value and saving values into arrays. The problem is that it's unable to read the value and there is no logic error.

Code

    '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 file

<?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>

Problem When reading the data, I'm poping up messagebox after each value is read. But the msgbox displays nothing. Same for the arrays, no data saved. It's blank.

Is there anything I'm missing or need to do in order to read the values. The same code in other project works fine.. Thankyou.

    '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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM