Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 从xml文件中删除节点会引发对象引用错误_Vb.net - Fatal编程技术网

Vb.net 从xml文件中删除节点会引发对象引用错误

Vb.net 从xml文件中删除节点会引发对象引用错误,vb.net,Vb.net,我有一个XML文件,看起来像 <?xml version="1.0"> <playlist> <name>My Playlist</name> <song> <name>Song Name Here</name> <path>Path to song here</path> <note>Song note

我有一个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
Private子MsItemRemoveClick(发送方作为System.Object,e作为EventArgs)处理msItemRemove。单击
如果lvwPlaylist.SelectedItems.Count>0,则
Dim xmlDoc作为新的XmlDocument
加载(_playlpath&lblPlaylistName.Text&“.xml”)
将歌曲调暗为xmlement=xmlDoc。选择SingleNode(“歌曲”)
对于lvwPlaylist.SelectedItems中的每个项目作为ListViewItem
对于歌曲中的每个节点作为XmlElement
如果node.SelectSingleNode(“name”).InnerText=item.SubItems(0)。Text,则

MsgBox(node.SelectSingleNode(“name”).InnerText)为了清晰起见,去掉了列表视图

对于此XML文件

<?xml version="1.0"?>
<playlist>
     <name>My Playlist</name>
     <song>
         <name>Alpha song</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>Beta song</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>Charlie song</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>Delta song</name>
         <path>Path to song here</path>
         <note>Song notes here</note>
         <artist>Song artist here</artist>
         <type>Song type here</type>
     </song>
</playlist>

非常感谢。我将研究这段代码,看看在使用其他函数err subs时能从中学到什么。
Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
  Dim strFilenameIn As String = "C:\Junk\Junk1.xml"
  Dim strFilenameOut As String = "C:\Junk\Junk2.xml"

  Dim lstNames As New List(Of String)
  lstNames.Add("Beta song")
  lstNames.Add("Charlie song")

  Dim xmlDoc As New XmlDocument
  xmlDoc.Load(strFilenameIn)
  For Each strSongName As String In lstNames
    Dim xnl As XmlNodeList = xmlDoc.SelectNodes("/playlist/song/name")
    For i As Integer = xnl.Count - 1 To 0 Step -1
      Dim xnd As XmlNode = xnl(i)
      If xnd.FirstChild.Value = strSongName Then 'match'
        xmlDoc.DocumentElement.RemoveChild(xnd.ParentNode)
      End If
    Next
  Next strSongName
  xmlDoc.Save(strFilenameOut)
  MsgBox("Finished")
End Sub