Imports System
Imports System.Xml
' This Imports statement assumes that the project's root namespace is
' "ApiSamples" and the name of the Bing API web reference is
' "net.bing.api". Modify this Imports statement as necessary.
Imports ApiSamples.net.bing.api
' Bing API 2.0 code sample demonstrating the use of the
' RelatedSearch SourceType over the SOAP Protocol.
Class RelatedSearchSample
' Replace the following string with the AppId you received from the
' Bing Developer Center.
Const AppId As String = "Insert your AppId here"
Shared Sub Main()
' BingService implements IDisposable.
Using service As BingService = New BingService
Try
Dim request As SearchRequest = BuildRequest()
' Send the request; display the response.
Dim response As SearchResponse = service.Search(request)
DisplayResponse(response)
Catch ex As System.Web.Services.Protocols.SoapException
' A SOAP Exception was thrown. Display error details.
DisplayErrors(ex.Detail)
Catch ex As System.Net.WebException
' An exception occurred while accessing the network.
Console.WriteLine(ex.Message)
End Try
End Using
End Sub
Shared Function BuildRequest() As SearchRequest
Dim request As New SearchRequest
With request
' Common request fields (required)
.AppId = AppId
.Query = "free"
.Sources = New SourceType() {SourceType.RelatedSearch}
' Common request fields (optional)
.Version = "2.0"
.Market = "en-us"
.Options = New SearchOption() {SearchOption.EnableHighlighting}
End With
Return request
End Function
Shared Sub DisplayResponse(ByVal response As SearchResponse)
' Display the results header.
Console.WriteLine("Bing API Version " & response.Version)
Console.WriteLine( _
"RelatedSearch results for " & response.Query.SearchTerms)
Console.WriteLine()
' Display the RelatedSearch results.
Dim builder As New System.Text.StringBuilder
Dim result As RelatedSearchResult
For Each result In response.RelatedSearch.Results
builder.Length = 0
builder.AppendLine(result.Title)
builder.AppendLine(result.Url)
DisplayTextWithHighlighting(builder.ToString)
Console.WriteLine()
Next
End Sub
Shared Sub DisplayTextWithHighlighting(ByVal [text] As String)
' Write text to the standard output stream, changing the console
' foreground color as highlighting characters are encountered.
Dim c As Char
For Each c In [text].ToCharArray
If (c = Char.ConvertFromUtf32(&HE000)) Then
' If the current character is the begin highlighting
' character (U+E000), change the console foreground color
' to green.
Console.ForegroundColor = ConsoleColor.Green
ElseIf (c = Char.ConvertFromUtf32(&HE001)) Then
' If the current character is the end highlighting
' character (U+E001), revert the console foreground color
' to gray.
Console.ForegroundColor = ConsoleColor.Gray
Else
Console.Write(c)
End If
Next
End Sub
Shared Sub DisplayErrors(ByVal errorDetails As XmlNode)
' Add the default namespace to the namespace manager.
Dim nsmgr As New XmlNamespaceManager( _
errorDetails.OwnerDocument.NameTable)
nsmgr.AddNamespace( _
"api", _
"http://schemas.microsoft.com/LiveSearch/2008/03/Search")
Dim errors As XmlNodeList = errorDetails.SelectNodes( _
"./api:Errors/api:Error", _
nsmgr)
If (Not errors Is Nothing) Then
' Iterate over the list of errors and display error details.
Console.WriteLine("Errors:")
Console.WriteLine()
Dim [error] As XmlNode
For Each [error] In errors
Dim detail As XmlNode
For Each detail In [error].ChildNodes
Console.WriteLine(detail.Name & ": " & detail.InnerText)
Next
Console.WriteLine()
Next
End If
End Sub
End Class