Prince Tegaton Prince Tegaton - 29 days ago 10
Vb.net Question

Can I use "sql-like" queries with XML in VB.Net

Can I select nodes based on criteria by passing queries similar to SQL such as LIKE, CONTAIN,SUM etc in XML?

This is my data structure

<institutions>
<institution type="university" abbrv="absu" name="Abia State University (absu)" />
<institution type="university" abbrv="atbu" name="Abubakar Tafawa Balewa University (atbu)" />
<institution type="university" abbrv="achievers" name="Achievers University (achievers)" />
<institutions>


I want to be able to get institutions using LIKE and other keywords. Can i get the equivalent of the below statement for xml?

SELECT Institution FROM tblInstitutions WHERE Name LIKE '%Abia%'

Answer

using XML as a database

Use System.Xml.Linq namespace features

  Dim data As XElement =
     <institutions>
        <institution type="university" abbrv="absu" name="Abia State University (absu)"/>
        <institution type="university" abbrv="atbu" name="Abubakar Tafawa Balewa University (atbu)"/>
        <institution type="university" abbrv="achievers" name="Achievers University (achievers)"/>
     </institutions>

'Search entries where `name` contains "Abia"
Dim result As IEnumerable(Of XElement) = 
    From institution As XElement In data.<institution>
    Where institution.@name.Contains("Abia")
    Select institution

Use another LINQ methods Sum, Avg etc. LINQ to XML
Check also XML literals in vb.net XML Literals Overview (Visual Basic)

Comments