Solved

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

Posted on 2013-06-05
6
607 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
Comment Utility
do u want to apply the search only on elements or also attributes?
0
 

Author Comment

by:jrlittle86
Comment Utility
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
Comment Utility
\w/ cool
anything else u need?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 

Accepted Solution

by:
jrlittle86 earned 0 total points
Comment Utility
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
Comment Utility
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
Comment Utility
hello, i want to ask, how to make function search using xml in vb.net?
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

763 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now