从 xml 文件中删除节点会引发对象引用错误 [英] delete node from xml file throws me an object reference error

查看:21
本文介绍了从 xml 文件中删除节点会引发对象引用错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的 XML 文件

I have an XML file that looks like

<?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 文件中删除歌曲节点,但我无法找出出现错误的原因.我仍在学习视觉基础知识.

I am trying to delete the song node from the xml file but i can't figure out the cause of the error I'm getting. I'm still learning visual basics.

错误:未将对象引用设置为对象的实例.

这是我的代码

    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 节点 中的歌曲名称匹配,则删除 name 的父节点>

My efforts are to loop through all selected listview items and if the name of the song matches the name of the song in the songs node then delete the parent node of name

推荐答案

为了清晰起见,去掉了 ListView....

Stripped out the ListView for clarity....

对于这个 XML 文件...

For this XML file...

<?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>

保存在磁盘上为C:\Junk\Junk1.xml,这段代码会找到并删除中间的两个节点...

saved on disk as C:\Junk\Junk1.xml, this code will find and remove the two middle nodes...

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

这篇关于从 xml 文件中删除节点会引发对象引用错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆