【发布时间】:2014-01-10 15:05:44
【问题描述】:
我正在使用 Visual Studio 2010 并在 VB 中进行编码。
我有一个由 XML 文件填充的 ListBox。 我设法让“全部删除”工作,但我无法让“删除单曲”工作。不确定如何从节点列表中的节点获取属性值。 我需要将 Bookmark Elements 的 title 属性与 lstBookmarks.Text 匹配,该属性包含列表框所选项目的文本。
突出显示需要删除的位置(至少对于我的代码而言)。 只要有解释,我很乐意接受完全重写的代码。
我的 XML 看起来像这样
<Data>
<Bookmark title="Page 1" link="Some File Path Here" />
<Bookmark title="Page 2" link="Some Other File Path Here" />
</Data>
我的删除看起来像这样
Private Sub DeleteToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DeleteToolStripMenuItem.Click
If lstBookmarks.SelectedIndex = -1 Then
MessageBox.Show("There are no bookmarks to clear!")
ElseIf lstBookmarks.SelectedValue.ToString() = "" Then
MessageBox.Show("There are no bookmarks to clear!")
Else
Dim xmlFile As String = filePath & "Resources\bookmark.xml"
Dim XMLDoc As XmlDocument = New XmlDocument
Dim nodes As XmlNodeList
XMLDoc.Load(xmlFile)
nodes = XMLDoc.SelectNodes("Data")
Dim RootElement As XElement = XElement.Load(xmlFile)
Dim DataElement As XmlElement = XMLDoc.DocumentElement
Dim NewElement As XmlElement = XMLDoc.CreateElement("Bookmark")
Dim FindElement = RootElement.<Bookmark>.Attributes("title")
If DataElement.HasChildNodes Then
For Each Attribute In FindElement
If Attribute = lstBookmarks.Text Then
'************************************************
'Match found, delete node or XML Element here
'************************************************
Else
'No Match in XML, no need to delete
End If
Next
End If
End If
End Sub
【问题讨论】:
标签: xml vb.net nodes xmlnode nodelist