读取 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>pathbinDebugresicon.png</iconPath>
<accountName>asa</accountName>
<accountPath>accountsasa</accountPath>
</dt_Accounts>
<dt_Accounts>
<accountID>1</accountID>
<iconPath>pathbinDebugresimageicon.png</iconPath>
<accountName>drav</accountName>
<accountPath>accountsdrav</accountPath>
</dt_Accounts>
</dsAccounts>

问题读取数据时,读取每个值后都会弹出消息框。但是味精框什么也没显示。数组相同,不保存数据。它是空白的。

我是否缺少或需要做什么才能读取值。其他项目中的相同代码工作正常。 谢谢。

'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

最新更新