我正在尝试制作一个程序,该程序读取一个选定的XML文件,另一个程序将其现在正在播放的歌曲和艺术家写入每首歌曲的开头。
这是我到目前为止所做的,但是我需要连续循环的代码,以便在自动加载新歌并编辑XML文件时,它也将在此更新。
Imports System
Imports System.Xml
Public Class MainForm
'Variables Declared'
'Saves file path as path'
Public XMLPath As String
'Full path to selected file'
Dim Path As String = Me.XMLPath
Private Sub LoadXMLButton_Click(sender As Object, e As EventArgs) Handles LoadXML_Button.Click
'Filehandle to to open dialog
Dim XMLPathFileHandle As OpenFileDialog = New OpenFileDialog
'Title of dialog box
XMLPathFileHandle.Title = "Select XML file"
'File types which file dialog accepts
XMLPathFileHandle.Filter = "All files (*.xml)|*.xml"
'if user clicks ok
If XMLPathFileHandle.ShowDialog() = Windows.Forms.DialogResult.OK Then
'save selected file path
XMLPath = XMLPathFileHandle.FileName
'show file path in combobox
ComboBox1.Text = XMLPath
End If
End Sub
Private Sub StartButton_Click(sender As Object, e As EventArgs) Handles StartButton.Click
'check if file exists
If (IO.File.Exists(XMLPath)) Then
'create a new xmltextreader object
'this is the object that we will loop and will be used to read the xml file
Dim document As XmlReader = New XmlTextReader(XMLPath)
'loop through the xml file
While (document.Read())
Dim type = document.NodeType
'if node type was element
If (type = XmlNodeType.Element) Then
'if the loop found a <ARTIST> tag
If (document.Name = "ARTIST") Then
TextBox1.Text = document.ReadInnerXml.ToString()
End If
'if the loop found a <TITLE> tag
If (document.Name = "TITLE") Then
TextBox2.Text = document.ReadInnerXml.ToString()
End If
End If
End While
'close XML to avoid error with saving
document.Close()
Else
MessageBox.Show("The filename you selected was not found.")
End If
End Sub
答案 0 :(得分:-1)
示例代码
- 首先添加FileSystemWatcher(来自工具箱)
Imports System
导入System.Xml Imports System.IO
Public Class Form1
'Variables Declared'
Dim XMLPath, Path_filename, Path_url As String
Private Sub LoadXMLButton_Click(sender As Object, e As EventArgs) Handles LoadXML_Button.Click
'Filehandle to to open dialog
Dim XMLPathFileHandle As OpenFileDialog = New OpenFileDialog
'Title of dialog box
XMLPathFileHandle.Title = "Select XML file"
'File types which file dialog accepts
XMLPathFileHandle.Filter = "All files (*.xml)|*.xml"
'if user clicks ok
If XMLPathFileHandle.ShowDialog() = Windows.Forms.DialogResult.OK Then
'save selected file path
XMLPath = XMLPathFileHandle.FileName
'show file path in combobox
XMLdirectory.Text = XMLPath
Dim filePath As String = XMLPathFileHandle.FileName
Dim directoryPath As String = Path.GetDirectoryName(filePath)
Path_url = directoryPath
Path_filename = XMLPathFileHandle.SafeFileName
''********* ADD THIS AREA
FileSystemWatcher1.Path = Path_url
''For file watch start
FileSystemWatcher1.EnableRaisingEvents = True
''*******************************
End If
End Sub
' Add a FileSystemWatcher object from toolbox
Private Sub FileSystemWatcher1_Changed(sender As Object, e As FileSystemEventArgs) Handles FileSystemWatcher1.Changed
TextBox1.Text = " file changed "
'processs()
End Sub
Private Sub processs()
Dim document As XmlReader = New XmlTextReader(XMLPath)
'check if file exists
If (IO.File.Exists(XMLPath)) Then
Dim type = document.NodeType
'if node type was element
If (type = XmlNodeType.Element) Then
'if the loop found a <ARTIST> tag
If (document.Name = "ARTIST") Then
TextBox1.Text = document.ReadInnerXml.ToString()
End If
'if the loop found a <TITLE> tag
If (document.Name = "TITLE") Then
TextBox2.Text = document.ReadInnerXml.ToString()
End If
End If
'close XML to avoid error with saving
document.Close()
End If
End Sub
结束班
我认为你想要吗?试试吧。如果您有问题,我会检查。
这是runnig代码