我有一个看起来像的 XML 文件
<?xml version="1.0">
<playlist>
<name>My Playlist</name>
<song>
<name>Song Name Here</name>
<path>Path to song here</path>
<note>Song notes here</note>
<artist>Song artist here</artist>
<type>Song type here</type>
</song>
<song>
<name>Song Name Here</name>
<path>Path to song here</path>
<note>Song notes here</note>
<artist>Song artist here</artist>
<type>Song type here</type>
</song>
</playlist>
我正在尝试从 xml 文件中删除歌曲节点,但我无法找出错误的原因。我还在学习视觉基础知识。
错误:对象引用未设置为对象的实例。
这是我的代码
Private Sub MsItemRemoveClick(sender As System.Object, e As EventArgs) Handles msItemRemove.Click
If lvwPlaylist.SelectedItems.Count > 0 Then
Dim xmlDoc As New XmlDocument
xmlDoc.Load(_playlistpath & lblPlaylistName.Text & ".xml")
Dim songs As XmlElement = xmlDoc.SelectSingleNode("song")
For Each item As ListViewItem In lvwPlaylist.SelectedItems
For Each node As XmlElement In songs
If node.SelectSingleNode("name").InnerText = item.SubItems(0).Text Then
MsgBox(node.SelectSingleNode("name").InnerText) '<------ this is where the error pops up on 'node.ParentNode.RemoveAll()
End If
Next
item.Remove()
Next
xmlDoc.Save(_playlistpath & lblPlaylistName.Text & ".xml")
End If
End Sub
我的努力是遍历所有选定的列表视图项目,如果歌曲名称与歌曲名称匹配,songs node
则删除name