读取XML文件VB.NET时为空值

时间:2018-08-27 11:04:48

标签: .net xml 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不显示任何内容。阵列相同,不保存数据。它是空白。

是否有任何我想念或需要做的事情才能读取这些值。其他项目中的相同代码也可以正常工作。 谢谢。

1 个答案:

答案 0 :(得分:1)

    '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