How to search in an XML file

XML files are made up of tags that contains information. The .Net technology is widely supported XML file format. Also the Dataset in ADO.NET uses XML format as its internal storage format.

The following source code shows how to search an item in an XML file using Dataset . Here Dataset using an XmlReader for read the content of the file. Locate the XML file using XmlReader and pass the XmlReader as argument of Dataset. By using the Dataset , search the product Product2 in the file Product.XML with the help of DataView.

Imports System.Xml
Public Class Form1
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim xmlFile As XmlReader
        xmlFile = XmlReader.Create("Product.xml", New XmlReaderSettings())
        Dim ds As New DataSet
        Dim dv As DataView

        dv = New DataView(ds.Tables(0))
        dv.Sort = "Product_Name"
        Dim index As Integer = dv.Find("Product2")

        If index = -1 Then
            MsgBox("Item Not Found")
            MsgBox(dv(index)("Product_Name").ToString() & "  " & dv(index)("Product_Price").ToString())
        End If

    End Sub

Click here to download the input file product.xml (C) 2021    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About