Visual Studio 2010 を使用し、VB でコーディングしています。
XML ファイルが取り込まれた ListBox があります。「すべて削除」を機能させることができましたが、「単一削除」を機能させることができません。ノードリスト内のノードから属性値を取得する方法がわかりません。ブックマーク要素のタイトル属性を、リストボックスで選択された項目のテキストを保持する 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