Imports System
Imports System.Net
Imports System.Xml
' Bing API 2.2 code sample demonstrating the use of the
' Translation SourceType over the XML Protocol.
Class TranslationSample
' 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()
Dim request As HttpWebRequest = BuildRequest()
Try
' Send the request; display the response.
Dim response As HttpWebResponse = DirectCast( _
request.GetResponse, _
HttpWebResponse)
DisplayResponse(response)
Catch ex As WebException
' An exception occurred while accessing the network.
Console.WriteLine(ex.Message)
End Try
End Sub
Shared Function BuildRequest() As HttpWebRequest
Dim requestString As String = "http://api.bing.net/xml.aspx?"
' Common request fields (required)
requestString &= _
"AppId=" & AppId _
& "&Query=espero" _
& "&Sources=Translation"
' Common request fields (optional)
requestString &= _
"&Version=2.2"
' SourceType-specific request fields (required)
requestString &= _
"&Translation.SourceLanguage=es" _
& "&Translation.TargetLanguage=en"
' Create and initialize the request.
Dim request As HttpWebRequest = DirectCast( _
WebRequest.Create(requestString), HttpWebRequest)
Return request
End Function
Shared Sub DisplayResponse(ByVal response As HttpWebResponse)
' Load the response into an XmlDocument.
Dim document As New XmlDocument
document.Load(response.GetResponseStream)
' Add the default namespace to the namespace manager.
Dim nsmgr As New XmlNamespaceManager(document.NameTable)
nsmgr.AddNamespace( _
"api", _
"http://schemas.microsoft.com/LiveSearch/2008/04/XML/element")
Dim errors As XmlNodeList = document.DocumentElement.SelectNodes( _
"./api:Errors/api:Error", _
nsmgr)
If (errors.Count > 0) Then
' There are errors in the response. Display error details.
DisplayErrors(errors)
Else
' There were no errors in the response. Display the
' Translation results.
DisplayResults(document.DocumentElement, nsmgr)
End If
End Sub
Shared Sub DisplayResults(ByVal root As XmlNode, _
ByVal nsmgr As XmlNamespaceManager)
Dim version As String = root.SelectSingleNode( _
"./@Version", _
nsmgr).InnerText
Dim searchTerms As String = root.SelectSingleNode( _
"./api:Query/api:SearchTerms", _
nsmgr).InnerText
' Display the results header.
Console.WriteLine("Bing API Version " & version)
Console.WriteLine("Translation results for " & searchTerms)
Console.WriteLine()
' Add the Translation SourceType namespace to the namespace manager.
nsmgr.AddNamespace( _
"tra", _
"http://schemas.microsoft.com/LiveSearch/2008/04/XML/translation")
Dim results As XmlNodeList = root.SelectNodes( _
"./tra:Translation/tra:Results/tra:TranslationResult", _
nsmgr)
' Display the Translation results.
Dim result As XmlNode
For Each result In results
Console.WriteLine(result.SelectSingleNode("./tra:TranslatedTerm", _
nsmgr).InnerText)
Next
End Sub
Shared Sub DisplayErrors(ByVal errors As XmlNodeList)
' 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 Sub
End Class