?
Solved

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

Posted on 2013-06-05
6
Medium Priority
?
647 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
Industry Leaders: 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!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

752 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