Search for string in XML file

ma701ss

Member
Joined
Dec 2, 2012
Messages
9
Programming Experience
1-3
Hi,

I need to search for a specific word in an XML file, entered by a user into a search box. For example:

VB.NET:
<products>

<product id="123">
<description>This is a product and it's great</description>
</product>

<product id="124">
<description>This is another product and it's also great</description>
</product>


<product id="125">
<description>This is yet another product and it's the best</description>
</product>

</products>

A very basic example above but if I want to search for the string "This" and generate a list of all product id's where the string appears, how can I do this in vb.net?

Thanks
 
Hi,

There are numerous ways to do this sort of thing. Here are two quick examples both using the XDocument class to demonstrate what you want to do:-

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim myXMLDoc As XDocument = XDocument.Load("YourFileNameHere")
 
  'Get the information you need using a conventional For Loop and an If Statement
  For Each currentElement As XElement In myXMLDoc...<product>
    If currentElement.<description>.Value.StartsWith("This") Then
      MsgBox(currentElement.@id)
    End If
  Next
 
  'Get the information you need using LINQ to XML and Lambda Expressions
  Dim myIDStrings As String() = myXMLDoc...<product>.Where(Function(x) x.<description>.Value.StartsWith("This")).Select(Function(x) x.@id).ToArray
  MsgBox(String.Join(", ", myIDStrings))
End Sub


Hope that helps.

Cheers,

Ian
 
Another one, through Linq. Pretty much the same as the second one from Ian above but using the other syntax:

Dim xDoc = XDocument.Load(xmlPath)
Dim keyWord = "This"

Dim resultIds = From p In xDoc...<product>
                Where p.<description>.First.Value.Contains(keyWord)
                Select p.@id


Also you might want to make the search case insensitive:

Dim xDoc = XDocument.Load(xmlPath)
Dim keyWord = "This"

Dim resultIds = From p In xDoc...<product>
                Where p.<description>.First.Value.ToUpper.Contains(keyWord.ToUpper)
                Select p.@id
 
Hi,

There are numerous ways to do this sort of thing. Here are two quick examples both using the XDocument class to demonstrate what you want to do:-

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  Dim myXMLDoc As XDocument = XDocument.Load("YourFileNameHere")
 
  'Get the information you need using a conventional For Loop and an If Statement
  For Each currentElement As XElement In myXMLDoc...<product>
    If currentElement.<description>.Value.StartsWith("This") Then
      MsgBox(currentElement.@id)
    End If
  Next
 
  'Get the information you need using LINQ to XML and Lambda Expressions
  Dim myIDStrings As String() = myXMLDoc...<product>.Where(Function(x) x.<description>.Value.StartsWith("This")).Select(Function(x) x.@id).ToArray
  MsgBox(String.Join(", ", myIDStrings))
End Sub


Hope that helps.

Cheers,

Ian

Hi Ian, I'm getting the following error and can't figure out why:

[FONT=Arial, Helvetica, Geneva, SunSans-Regular, sans-serif]BC30506: Handles clause requires a WithEvents variable defined in the containing type or one of its base types.[/FONT]

Thanks
 
Hi,

I have got no idea what that error has got to do with reading an XML file and if this is completely unrelated to the XML file question that you asked then you need to create a new thread to ask this question showing the relevant code that has produced the error so that we can help you better.

However, and at a guess, you have created a Variable of an Object Type which can raise Events and then added that Variable to the Handles Clause of a Subroutine. i.e:-

Private tBox As New TextBox
 
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles tBox.Click
End Sub


If you do NOT declare the above Variable with the WithEvents keyword then you CANNOT declare a handles clause as demonstrated above and you need to add the Handler manually using the AddHandler Statement.

Hope that helps.

Cheers,

Ian
 
Last edited:
Hi,

I have got no idea what that error has got to do with reading an XML file and if this is completely unrelated to the XML file question that you asked then you need to create a new thread to ask this question showing the relevant code that has produced the error so that we can help you better.

However, and at a guess, you have created a Variable of an Object Type which can raise Events and then added that Variable to the Handles Clause of a Subroutine. i.e:-

Private tBox As New TextBox
 
Private Sub TextBox1_Click(sender As Object, e As EventArgs) Handles tBox.Click
End Sub


If you do NOT declare the above Variable with the WithEvents keyword then you CANNOT declare a handles clause as demonstrated above and you need to add the Handler manually using the AddHandler Statement.

Hope that helps.

Cheers,

Ian

Thanks, it was an error relating to the code you posted, not a separate problem. Still relating to above, if my XML file is structured as follows:

<Category>
<Product>
<Description></Description>
</Product>
<Category>

Do I need to alter the code or will myXMLDoc...<product> still work without mentioning <category>?
 
Thanks, it was an error relating to the code you posted, not a separate problem. Still relating to above, if my XML file is structured as follows:

<Category>
<Product>
<Description></Description>
</Product>
<Category>

Do I need to alter the code or will myXMLDoc...<product> still work without mentioning <category>?

Hi,

So long as that is supposed to be a Closing Category Tag then, Yes, everything should work fine based on the examples already shown.

Cheers,

Ian
 
Back
Top