Imports System.IO
Imports System.Xml
'I didnt take the time to write this, but i did test it and it works. Please let me know if this helps. All i have on the form is just two listboxes. The
'content in the xml files and the results this code gives is shown in comments below
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim xm As New XMLfiles_Flagged()
Dim flaggeds() As String = xm.getFlaggedFiles
Dim files() As String = xm.getAllFiles
ListBox2.DataSource = files
ListBox1.DataSource = flaggeds
End Sub
Class XMLfiles_Flagged
Private rootLocation As String
Private xmlFileLocations As New ArrayList
Private xmlFileLocations_Flagged As New ArrayList
ReadOnly Property getAllFiles As String()
Get
Return CType(xmlFileLocations.ToArray(GetType(String)), String())
End Get
End Property
ReadOnly Property getFlaggedFiles() As String()
Get
Return CType(xmlFileLocations_Flagged.ToArray(GetType(String)), String())
End Get
End Property
Sub New(Optional ByVal location As String = "C:\XMLfiles")
rootLocation = location
Dim dirRootLoc As New DirectoryInfo(rootLocation)
recursiveGetXMLFiles(dirRootLoc)
flagXMLFiles()
End Sub
Private Sub flagXMLFiles()
For Each xmlLoc As String In xmlFileLocations
If Not xmlContainsValue(xmlLoc, "firstname", "Tom") Then
xmlFileLocations_Flagged.Add(xmlLoc)
End If
Next
End Sub
Private Function xmlContainsValue(ByVal xmlFileLocation As String, ByVal element As String, ByVal matchValue As String) As Boolean
Dim matches As Boolean = False
'The code below enclosed in ---------vvv------
'is from http://www.codeproject.com/KB/cpp/parsefilecode.aspx 'I TAKE NO CREDIT FOR IT 'modified a bit of it though
Dim m_xmlr As XmlTextReader
'Create the XML Reader
m_xmlr = New XmlTextReader(xmlFileLocation)
'Disable whitespace so that you don't have to read over whitespaces
m_xmlr.WhitespaceHandling = WhitespaceHandling.None
'read the xml declaration and advance to family tag
m_xmlr.Read()
'read the family tag
m_xmlr.Read()
'Load the Loop
While Not m_xmlr.EOF
'Go to the name tag
m_xmlr.Read()
'if not start element exit while loop
If Not m_xmlr.IsStartElement() Then
Exit While
End If
'Get the Gender Attribute Value
Dim genderAttribute = m_xmlr.GetAttribute("gender")
'Read elements firstname and lastname
m_xmlr.Read()
'Get the firstName Element Value
Dim firstNameValue = m_xmlr.ReadElementString(element)
If firstNameValue = matchValue Then
matches = True
Else
matches = False
End If
Exit While 'not the best way to do this..
End While
'close the reader
m_xmlr.Close()
'------^^^---------'
Return matches
End Function
Private Sub recursiveGetXMLFiles(ByVal rootDir As DirectoryInfo)
Dim files() As FileInfo = rootDir.GetFiles
For Each File As FileInfo In files
If File.Extension = ".xml" Then
xmlFileLocations.Add(File.FullName)
End If
Next
If rootDir.GetDirectories.Length > 0 Then
For Each subDir As DirectoryInfo In rootDir.GetDirectories
recursiveGetXMLFiles(subDir)
Next
End If
End Sub
End Class
'These are a list of files in the directory
'C:\XMLfiles\file1.xml
'C:\XMLfiles\file2.xml
'C:\XMLfiles\Some\New folder\file1 (2).xml
'C:\XMLfiles\Some\New folder\file1.xml
'C:\XMLfiles\Some\New folder\asdf\file1.xml
'C:\XMLfiles\Some - Copy\New folder\asdf\file1 (2).xml
'C:\XMLfiles\Some - Copy\New folder\asdf\file1.xml
'C:\XMLfiles\Some - Copy\Some\file1 (2).xml
'C:\XMLfiles\Some - Copy\Some\file1.xml
'C:\XMLfiles\Some - Copy\Some\New folder\asdf\file1.xml
'These two files are returned as flagged
'C:\XMLfiles\file2.xml
'C:\XMLfiles\Some - Copy\Some\New folder\asdf\file1.xml
'this is xml content of file1.xml that was flagged
' <?xml version="1.0" encoding="UTF-8"?>
'<family>
' <name gender="Male">
' <firstname>ToNNm</firstname>
' <lastname>Smith</lastname>
' </name>
' <name gender="Female">
' <firstname>Dale</firstname>
' <lastname>Smith</lastname>
' </name>
'</family>
'this is the file2.xml that was flagged
' <?xml version="1.0" encoding="UTF-8"?>
'<family>
' <name gender="Male">
' <firstname>Tommy</firstname>
' <lastname>Smith</lastname>
' </name>
' <name gender="Female">
' <firstname>Dale</firstname>
' <lastname>Smith</lastname>
' </name>
'</family>
'ALL of the rest are this exact xml file
' <?xml version="1.0" encoding="UTF-8"?>
'<family>
' <name gender="Male">
' <firstname>Tom</firstname>
' <lastname>Smith</lastname>
' </name>
' <name gender="Female">
' <firstname>Dale</firstname>
' <lastname>Smith</lastname>
' </name>
'</family>
End Class