Solved

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

Posted on 2013-06-05
6
626 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
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

685 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