XML Code Samples (Image SourceType)

Bing

This topic contains code samples that produces an XML request for the Image SourceType. For more information, see Image SourceType (Bing, Version 2.0).

Requirements

  • A deployment computer with an Internet connection

  • The ability to send requests using Hyper Text Transfer Protocol (HTTP 1.1)

  • The ability to parse XML

Demonstrates

These code samples demonstrate how to:

  • Send a request to the Bing XML interface and the Image SourceType

  • Display the Bing response as results

The samples are written in both Visual Basic and C#.

Example

using System;
using System.Net;
using System.Xml;

// Bing API 2.0 code sample demonstrating the use of the
// Image SourceType over the XML Protocol.
static class ImageSample
{
    // Replace the following string with the AppId you received from the
    // Bing Developer Center.
    const string AppId = "Insert your AppId here";

    static void Main()
    {
        HttpWebRequest request = BuildRequest();

        try
        {
            // Send the request; display the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            DisplayResponse(response);
        }
        catch (WebException ex)
        {
            // An exception occurred while accessing the network.
            Console.WriteLine(ex.Message);
        }
    }

    static HttpWebRequest BuildRequest()
    {
        string requestString = "http://api.bing.net/xml.aspx?"

            // Common request fields (required)
            + "AppId=" + AppId
            + "&Query=xbox site:microsoft.com"
            + "&Sources=Image"

            // Common request fields (optional)
            + "&Version=2.0"
            + "&Market=en-us"
            + "&Adult=Moderate"

            // Image-specific request fields (optional)
            + "&Image.Count=10"
            + "&Image.Offset=0";

        // Create and initialize the request.
        HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(
            requestString);

        return request;
    }

    static void DisplayResponse(HttpWebResponse response)
    {
        // Load the response into an XmlDocument.
        XmlDocument document = new XmlDocument();
        document.Load(response.GetResponseStream());
        
        // Add the default namespace to the namespace manager.
        XmlNamespaceManager nsmgr = new XmlNamespaceManager(
            document.NameTable);
        nsmgr.AddNamespace(
            "api",
            "http://schemas.microsoft.com/LiveSearch/2008/04/XML/element");

        XmlNodeList errors = document.DocumentElement.SelectNodes(
            "./api:Errors/api:Error",
            nsmgr);

        if (errors.Count > 0)
        {
            // There are errors in the response. Display error details.
            DisplayErrors(errors);
        }
        else
        {
            // There were no errors in the response. Display the
            // Image results.
            DisplayResults(document.DocumentElement, nsmgr);
        }
    }

    static void DisplayResults(XmlNode root, XmlNamespaceManager nsmgr)
    {
        // Add the Image SourceType namespace to the namespace manager.
        string imageNamespaceUri =
            "http://schemas.microsoft.com/LiveSearch/2008/04/XML/multimedia";
        nsmgr.AddNamespace("mms", imageNamespaceUri);

        XmlNode image = root.SelectSingleNode("./mms:Image", nsmgr);
        XmlNodeList results = image.SelectNodes(
            "./mms:Results/mms:ImageResult",
            nsmgr);

        string version = root.SelectSingleNode("./@Version", nsmgr).InnerText;
        string searchTerms = root.SelectSingleNode(
            "./api:Query/api:SearchTerms",
            nsmgr).InnerText;
        int offset;
        int.TryParse(
            image.SelectSingleNode("./mms:Offset", nsmgr).InnerText,
            out offset);
        int total;
        int.TryParse(
            image.SelectSingleNode("./mms:Total", nsmgr).InnerText,
            out total);

        // Display the results header.
        Console.WriteLine("Bing API Version " + version);
        Console.WriteLine("Image results for " + searchTerms);
        Console.WriteLine(
            "Displaying {0} to {1} of {2} results",
            offset + 1,
            offset + results.Count,
            total);
        Console.WriteLine();

        // Display the Image results.
        foreach (XmlNode result in results)
        {
            Console.WriteLine(
                result.SelectSingleNode("./mms:MediaUrl", nsmgr).InnerText);
            Console.WriteLine(
                "Page Title: "
                + result.SelectSingleNode("./mms:Title", nsmgr).InnerText);
            Console.WriteLine(
                "Page URL: "
                + result.SelectSingleNode("./mms:Url", nsmgr).InnerText);
            Console.WriteLine(
                "Dimensions: "
                + result.SelectSingleNode("./mms:Width", nsmgr).InnerText
                + "x"
                + result.SelectSingleNode("./mms:Height", nsmgr).InnerText);

            string thumbnailUrl = result.SelectSingleNode(
                "./mms:Thumbnail/mms:Url",
                nsmgr).InnerText;
            Console.WriteLine("Thumbnail URL: " + thumbnailUrl);

            Console.WriteLine();
        }
    }

    static void DisplayErrors(XmlNodeList errors)
    {
        // Iterate over the list of errors and display error details.
        Console.WriteLine("Errors:");
        Console.WriteLine();
        foreach (XmlNode error in errors)
        {
            foreach (XmlNode detail in error.ChildNodes)
            {
                Console.WriteLine(detail.Name + ": " + detail.InnerText);
            }

            Console.WriteLine();
        }
    }
}

Show: