Solved

Search for a String within an XML Element using VB.Net

Posted on 2013-06-05
6
619 Views
Last Modified: 2016-05-17
What is the method in VB.Net for searching for a substring within an element in an XML file?

Here's an example.

In the following XML file - search for the string "Smith" in the <Entry> node and display a msgbox that says TRUE (or whatever) if found.

In this case, the string "Smith" shows up in both John T. Smith, Jr and "Smithson Jones". Would it be same as if I was looking for the string "Administration" in the <Department> node?

---Sample XML Below---

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<CustomerLog>
  <CustomerNumber>2013</CustomerNumber>
  <Department>Security Administration</Department>
  <Location>Podunk</Location>
  <Logs>
    <Entry Time="9:59:50 PM">Edward Scissorhands</Entry>
    <Entry Time="9:48:45 PM">John T. Smith, Jr</Entry>
  </Logs>
</CustomerLog>
 
<CustomerLog>
  <CustomerNumber>3102</CustomerNumber>
  <Department>Personnel Administration</Department>
  <Location>Podunk</Location>
  <Logs>
    <Entry Time="9:09:50 AM">John T. Smith, Jr</Entry>
    <Entry Time="9:48:45 AM">Smithson Jones</Entry>
  </Logs>
</CustomerLog>

This is a follow-on to my preview question titled:  "XML File Search Using VB.Net"
0
Comment
Question by:jrlittle86
  • 3
  • 2
6 Comments
 
LVL 42

Expert Comment

by:sedgwick
ID: 39224703
do u want to apply the search only on elements or also attributes?
0
 

Author Comment

by:jrlittle86
ID: 39224761
Elements - Actually I got it and was getting ready to remove the post...Thanks for the quick reply though.

I simply added a Regex.IsMatch to the earlier function you replied with.

...
Dim AssocRegText As Regex = New Regex(String.Format("{0}", Associate))
Dim assoc As Boolean = If(associate = Nothing, True, AssocRegText.IsMatch(root.Element("Associate").Value.ToUpper))
...

Seems to be working as expected.
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39224770
\w/ cool
anything else u need?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

Accepted Solution

by:
jrlittle86 earned 0 total points
ID: 39230215
Here's the entire solution:

I've broken it down a little more for this example but the code can be simplified even more:

'Change to Upper Case so we don't have to worry about the Search being Case Sensitive
CustNum=CustNum.ToUpper
Location=Location.ToUpper
Associate=Associate.ToUpper

'Pass the Search Folder, Customer Number, Location, and Associate variables to the GetFilesByCriteria Function (kudos to Sedgwick for this).  This will return a list of filenames that meet this criteria

Dim SearchResult As List(Of String) = GetFilesByCriteria(SearchFolder, CustNum, Location, Associate)

---

Private Function GetFilesByCriteria(folder as string, custNumber As String, city As String, associate As String) As List(Of String)
        Dim files = Directory.GetFiles(folder, "*.XML", SearchOption.AllDirectories)
        Dim list As List(Of String) = New List(Of String)

        For Each file In files
            Dim root = XElement.Load(file)

            'Add a Regex filter for each variable pass to the function so we search only that text
            Dim CustNumRegText As Regex = New Regex(String.Format("{0}", custNumber))
            Dim CityRegText As Regex = New Regex(String.Format("{0}", city))
            Dim AssocRegText As Regex = New Regex(String.Format("{0}", associate))

            'See if the there is a Regex match for each variable
            Dim customerNumber As Boolean = If(custNumber = Nothing, True, CustNumRegText.IsMatch(root.Element("CustomerNumber").Value.ToUpper)
            Dim cityValue As Boolean = If(city = Nothing, True, CityRegText.IsMatch(root.Element("City").Value.ToUpper)
            Dim assoc As Boolean = If(associate = Nothing, True, AssocRegText.IsMatch(root.Element("Associate").Value.ToUpper)
         
             'If there is a match for each variable - add it to the list
             If ( customerNumber and cityValue and assoc) Then
                list.Add(file)
            End If
        Next
        Return list
    End Function

One note about Directory.GetFiles:  
Directory.GetFiles(folder, "*.XML", SearchOption.AllDirectories)

The option SearchOption.AllDirectories will return a System.Unauthorized Access exception if it encounters a folder the user has no rights to (try it by selecting c:\ as the search path and you'll see what I mean when you reach the recycle bin folder).

So, be aware that you may want to handle this exception.
0
 

Author Closing Comment

by:jrlittle86
ID: 39240445
I was able to complete this before getting a response. I thought I would show the complete solution for others.  Again, kudos to Sedgwick.
0
 

Expert Comment

by:ROKAYAH ABDUL RAHMAN
ID: 41598945
hello, i want to ask, how to make function search using xml in vb.net?
0

Featured Post

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question