Solved

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

Posted on 2013-06-05
6
629 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…

738 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