【问题标题】:Empty value when reading XML file VB.NET读取 XML 文件 VB.NET 时的空值
【发布时间】:2018-08-27 11:04:47
【问题描述】:

我正在尝试读取一个简单的 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 什么也没显示。数组也一样,没有保存数据。一片空白。

为了读取这些值,我有什么遗漏或需要做的吗?其他项目中的相同代码工作正常.. 谢谢。

【问题讨论】:

    标签: .net xml vb.net


    【解决方案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
    

    【讨论】:

      猜你喜欢
      • 2012-09-09
      • 1970-01-01
      • 2013-06-07
      • 1970-01-01
      • 2017-07-23
      • 2016-10-11
      • 2013-07-05
      • 2020-07-20
      • 1970-01-01
      相关资源
      最近更新 更多