Using the API with Microsoft .Net Framework using the XML interface

Bing

The most straightforward way to use the XML interface from .NET code is to use the HttpWebRequest class found in the System.Net namespace. This class enables you to make arbitrary HTTP GET requests, an ability that you need in order to invoke the XML interface. The code in Example 5 shows a simple usage of the XML API using HttpWebRequest.

Example 5: Invoking the XML Interface

string url = "http://api.search.live.net/xml.aspx?Appid={0}&sources={1}&query={2}";
string completeUri = String.Format(url, AppId, "image", "sushi");
HttpWebRequest webRequest = null;
webRequest = (HttpWebRequest)WebRequest.Create(completeUri);
HttpWebResponse webResponse = null;
webResponse  = (HttpWebResponse)webRequest.GetResponse();
XmlReader xmlReader = null;
xmlReader = XmlReader.Create(webResponse.GetResponseStream());

When using HttpWebRequest, you generally follow the pattern shown in Example 5. Passing the static WebRequest.Create method a valid URL returns an HttpWebRequest instance. In this code, we are creating the URL by using String.Format, passing in the SourceType and the Query value as well as the AppId. By calling HttpWebRequest.GetResponse, the HTTP request is made to the Bing API XML interface, which returns results once the HTTP request has completed. We can then take the stream and use the XML processing engine of your choice to process it. In this case, we’ve chosen XmlReader.

What you do with the results next is dependent on the XML processing code you’d like to write. On the .NET platform the choices are to continue to use the XmlReader, turn the XmlReader into an XmlDocument, or use XLINQ. If, for example, you wanted to use the results from your query and data bind the results to an ASP.NET, WebForms, or WPF control, you might choose to use XLINQ to query the results and project them into a collection of strongly-typed objects that could easily be used for data binding, as shown in Example 6.

Example 6: Processing XML interface Image SourceType Results Using XLINQ

XDocument data = XDocument.Load(xmlReader);
IEnumerable<XNode> nodes = null;
nodes  = data.Descendants(XName.Get("Results", IMAGE_NS)).Nodes();
if (nodes.Count() > 0)
{
    var results = from uris in nodes
                  select new LiveSearchResultImage
                  {
                      URI = 
((XElement)uris).Element(XName.Get("Url", IMAGE_NS)).Value,
                      Title = ((XElement)uris).Element(XName.Get("Title", IMAGE_NS)).Value,
                      ThumbnailURI = ((XElement)uris).Element(XName.Get("Thumbnail", IMAGE_NS)).Value,

                  };
    return results;
}

Using XLINQ is fairly easy, as we can use the XDocument.Descendants method to get at the Result element (again, note that the Result element is namespace qualified, as we have the Image XML namespace URI as a constant IMAGE_NS already defined in our code). In this case, we are projecting each XML ImageResult element (the child nodes under the Result element) into a .NET type we’ve defined called LiveSearchResultImage (see Example 7).

Example 7: LiveSearchResultImage Type Definition

public class LiveSearchResultImage
{
    public string Title { get; set; }
    public string Description { get; set; }
    public string URI { get; set; }
    public string ImageURI { get; set; }
    public string ThumbnailURI { get; set; }
}

Regardless of the XML processing interface you choose, processing the child elements under the Results element is the primary goal.

Show: