Jim Little
asked on
XML File Search Using VB.Net
I want to create a search in VB.Net for all XML Filenames that meet the Search Criteria entered by a user. The User can enter information in any of four textboxes.
I have a form in VB.Net that has the four textboxes named:
Customer Number
Customer Name
Associate
City
So, for example, if the User enters:
1234 for the Customer Number
Podunk for the City
Allen as the Associate
The result is a list of XML filenames that meet this criteria. In the example with the data below, the result would be:
Podunk_1234_May232013.XML
The XML data below is in the file: Podunk_1234_May232013.XML
<CustomerNotes>
<CustomerNumber>1234</Cust omerNumber >
<Customer>Johnny Brokencode</Customer>
<Associate>Allen Expert</Associate>
<City>Podunk</City>
<FileName>JohnnyBrokencode May232012.docx</FileName>
</CustomerNotes>
The XML contents of the following filename: Burgville_1234_April152013 .XML
<CustomerNotes>
<CustomerNumber>1234</Cust omerNumber >
<Customer>Johnny Brokencode</Customer>
<Associate>Polly Procedure</Associate>
<City>Burgville</City>
<FileName>JohnnyBrokencode April152012.docx</FileName >
</CustomerNotes>
My search Function works for a single search term fine using the following function (some parts are redacted for this post - but it works).
Private Function FileSearch(ByVal Filename as String, ByRef ResultText As String) as Boolean
Dim xDoc As New XmlDocument
xDoc.Load(Filename)
Dim xNodes As XmlNodeList
xNodes = xDoc.SelectNodes("Customer Notes")
Dim xNode As XmlNode
Dim FindCustomerNumber As String
Dim SearchString As String = ""
Dim Result As Boolean = False
ResultText = ""
For Each xNode In xNodes
SearchString = xNode.InnerText
If SearchString.IndexOf(FindC aseNum) >= 0 Then
Result = True
ResultText = xNode.OuterXml
End If
Next
If Result=False Then
ResultText = "No Files Found"
End If
Return Result
End Function
My question is - what methods would you recommend to be able to search an XML file (Innertext) for multiple search criteria? Having Nested loops seems just too messy (and possibly slow) with as many as 5 Search fields...
Thanks.
I have a form in VB.Net that has the four textboxes named:
Customer Number
Customer Name
Associate
City
So, for example, if the User enters:
1234 for the Customer Number
Podunk for the City
Allen as the Associate
The result is a list of XML filenames that meet this criteria. In the example with the data below, the result would be:
Podunk_1234_May232013.XML
The XML data below is in the file: Podunk_1234_May232013.XML
<CustomerNotes>
<CustomerNumber>1234</Cust
<Customer>Johnny Brokencode</Customer>
<Associate>Allen Expert</Associate>
<City>Podunk</City>
<FileName>JohnnyBrokencode
</CustomerNotes>
The XML contents of the following filename: Burgville_1234_April152013
<CustomerNotes>
<CustomerNumber>1234</Cust
<Customer>Johnny Brokencode</Customer>
<Associate>Polly Procedure</Associate>
<City>Burgville</City>
<FileName>JohnnyBrokencode
</CustomerNotes>
My search Function works for a single search term fine using the following function (some parts are redacted for this post - but it works).
Private Function FileSearch(ByVal Filename as String, ByRef ResultText As String) as Boolean
Dim xDoc As New XmlDocument
xDoc.Load(Filename)
Dim xNodes As XmlNodeList
xNodes = xDoc.SelectNodes("Customer
Dim xNode As XmlNode
Dim FindCustomerNumber As String
Dim SearchString As String = ""
Dim Result As Boolean = False
ResultText = ""
For Each xNode In xNodes
SearchString = xNode.InnerText
If SearchString.IndexOf(FindC
Result = True
ResultText = xNode.OuterXml
End If
Next
If Result=False Then
ResultText = "No Files Found"
End If
Return Result
End Function
My question is - what methods would you recommend to be able to search an XML file (Innertext) for multiple search criteria? Having Nested loops seems just too messy (and possibly slow) with as many as 5 Search fields...
Thanks.
here's a function which accept 3 criteria to look for the xml.
1st phase is having regex to match file names using the custmNumber and city arguments.
so for example, for customNumber 1234 and city NewYork, the regex matches xml files which the file name is in the following pattern:
NewYork_1234_ .... .xml
i didn't handle nullable arguments and stuff like that.
2nd phase is the load result xml from previous regex match and look for the associate name also passed as argument to the function.
Private Function GetFilesByCriteria(custNumber As String, city As String, associate As String) As List(Of String)
Dim regFileName As Regex = New Regex(String.Format("^{0}_{1}_", city, custNumber))
Dim files = Directory.GetFiles("c:\temp", "*.XML", SearchOption.AllDirectories).Where(Function(file) regFileName.IsMatch(Path.GetFileNameWithoutExtension(file)))
Dim list As List(Of String) = New List(Of String)
For Each file In files
Dim root = XElement.Load(file)
Dim assoc = root.Element("Associate").Value
If (assoc = associate) Then
list.Add(file)
End If
Next
Return list
End Function
the function logic has 2 phases:1st phase is having regex to match file names using the custmNumber and city arguments.
so for example, for customNumber 1234 and city NewYork, the regex matches xml files which the file name is in the following pattern:
NewYork_1234_ .... .xml
i didn't handle nullable arguments and stuff like that.
2nd phase is the load result xml from previous regex match and look for the associate name also passed as argument to the function.
ASKER
Clever...and very observant! Unfortunately - this wouldn't work in this case.
Here's why...
My XML filename example (with the city and customer number in the XML filename) was a happy coincidence. I probably should have listed them as some random file name. The xml files can actually have any filename - and hence - that's why we must search on the contents.
A user really would search on any one of the fields or any combination of fields is this example...
ideas?
Here's why...
My XML filename example (with the city and customer number in the XML filename) was a happy coincidence. I probably should have listed them as some random file name. The xml files can actually have any filename - and hence - that's why we must search on the contents.
A user really would search on any one of the fields or any combination of fields is this example...
ideas?
ok, so regex is out of the window.
is it possible to have any combination of the 3 search criteria?
is it possible to have any combination of the 3 search criteria?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Awesome. Brilliant. I think I like Linq. Many thanks!!!
That would save the time to parse xml files for no reason.
Secondly id use xpath to get the values of the search criterias cause the xml structure is the same for all files.
If u need code sample let me know and ill post one for you.