Query XML APIs in vb.net

Hi
I would like to query and display results from XML APIs (links) using Visual basic. I will have a search box with search parameters so the users can search the xml APi and get data displayed back on the browser or just the summary of the query indicating results so that users can click on the link and be redirected to the external site to view the details of their search,
similar to comparison sites… Could you please advise on the best solution and approach for this? any examples, tutorials or samples to try first, would be great

Thanks

LINQ would probably be the simplest approach. The documentation is pretty solid with examples ranging from basic to advanced querying.

Here’s a starting point:
https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/concepts/linq/basic-queries-linq-to-xml

1 Like

Thank you. Will that enable me to do advanced stuff with API queries etc

Read my answer…or read the link :wink:

Ok I tried using one of the examples as shown below but I keep getting the error:

BC30002: Type ‘XElement’ is not defined.:

My other asp.net pages do work without the code below but it just doesnt like the Xelement even I imported all the namespaces. Any ideas where the issue is?

type <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<!-- Begin Main Section -->
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web.UI.WebControls" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Net.HttpWebResponse" %>
<%@ import Namespace="System.Linq" %>
<%@ import Namespace="System.Configuration"%>
<%@ Import Namespace="System.XML.XLinq"%>
<%@ Import Namespace="System.XML.Linq"%>
<%@ Import Namespace="System.Xml.XPath"%>



<Script runat="server">
sub Page_Load
Dim doc As XElement = XElement.Load("Test.xml")  
Dim childList As IEnumerable(Of XElement) = _  
        From el In doc.Elements() _  
        Select el  
For Each e As XElement In childList  
    Console.WriteLine(e)  
Next 



end sub
</Script>or paste code here
<%@ Import Namespace="System.XML.XLinq"%> 
<%@ Import Namespace="System.XML.Linq"%>
<%@ Import Namespace="System.Xml.XPath"%>

Two of these are wrong, one is right… :shifty:

I already tried lower case but in vain:

<%@ Import Namespace="System.Xml.XLinq"%>
<%@ Import Namespace="System.Xml.Linq"%>

That should have cleared that error…

Still getting the same error. Is this somethong not right on the server? Could you try it on your environment?

I don’t do vb.net. Just referring to the documentation

Though I did put your code into https://dotnetfiddle.net/ and was able to get it to “compile”

ok when i used the site you mentioned, i got loads of errors:

ompilation error (line 1, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 3, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 4, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 5, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 6, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 7, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 8, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 9, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 10, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 11, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 12, col 3): Keyword, identifier, or string expected after verbatim specifier: @
Compilation error (line 1, col 1): A namespace cannot directly contain members such as fields or methods

I managed to get it to compile on my local machine but it always print the word ‘type’ on the browser. It is supposed to print the xml file on the example. I definitely have the xml file and the the path configured correctly but it just prints the same word type. not sure why?

Are you sure your XML is valid? I took this sample code (which is from the first link I provided and basically is the same as yours) and used this code over at the tester I linked to earlier

Imports System
Imports System.Linq
Imports System.Xml.Linq
Imports System.Collections.Generic
Imports System.IO
				
Public Module Module1
	Public Sub Main()
		File.WriteAllText("Test.xml", "<Root>" + Environment.NewLine + _  
			"    <Child1>1</Child1>" + Environment.NewLine + _  
			"    <Child2>2</Child2>" + Environment.NewLine + _  
			"    <Child3>3</Child3>" + Environment.NewLine + _  
			"</Root>")  

		Console.WriteLine("Querying tree loaded with XDocument.Load")  
		Console.WriteLine("----")  
		Dim doc As XDocument = XDocument.Load("Test.xml")  
		Dim childList As IEnumerable(Of XElement) = _  
				From el In doc.Root.Elements() _  
				Select el  
		For Each e As XElement In childList  
			Console.WriteLine(e)  
		Next
	End Sub
End Module

And got this result

Querying tree loaded with XDocument.Load
----
<Child1>1</Child1>
<Child2>2</Child2>
<Child3>3</Child3>

I am using visual basic and not C#. I got the same code from that website
so may be the VB script they put on the site is not correct? you can try it

Uhmmm… Did you look at the code I posted? It’s the vb.net code, not c#

This is the code I am using. syntax is different:

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" Debug="true" %>
<!-- Begin Main Section -->
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System" %>
<%@ import Namespace="System.IO" %>
<%@ import Namespace="System.Web.UI.WebControls" %>
<%@ import Namespace="System.Xml" %>
<%@ import Namespace="System.Net.HttpWebResponse" %>
<%@ import Namespace="System.Linq" %>
<%@ import Namespace="System.Configuration"%>
<%@ Import Namespace="System.Xml.XLinq"%>
<%@ Import Namespace="System.Xml.Linq"%>
<%@ Import Namespace="System.Xml.XPath"%>



<Script runat="server">
sub Page_Load
Dim doc As XElement = XElement.Load("Test.xml")  
Dim childList As IEnumerable(Of XElement) = _  
        From el In doc.Elements() _  
        Select el  
For Each e As XElement In childList  
    Console.WriteLine(e)  
Next 



end sub
</Script>

I managed to get my code to work on my local machine and print the xml elements. It was mainly syntax issue so had to use: Response.Write(e)

However, on the server in the cloud, it still complains about the same line: BC30002: Type ‘XElement’ is not defined.

Make sure your latest code is there and not cached. That was definitely an import namespace issue

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.