Using Bing to do Spell Checking for You


I recently had to find a way to add spell checking to a web app. To be honest, I've seen about 20 different ways to do this and most of them weren't what I was looking for. The best option I saw was using Google. And normally I'd just use that code. I mean it's there, I don't have to do much, etc. But I had some spare time and for those of you that don't know me, I love to reinvent the wheel... I'm weird and I've accepted it. Another thing that you might want to know is I like trying out services that the average person isn't using. As such, I've been using Bing in recent months. I actually like it thus far, specifically the fact that for most of my searches it's location aware (type in Best Buy and along with their site, I get a map of stores around me, type traffic and I get the local traffic conditions, etc.). Google has been adding those features in more and more but it's still not quite there. Anyway, I thought to myself, I wonder if I can do the same thing in Bing? Turns out you can.

   1: public List<string> CheckSpelling(string Item)
   2: {
   3:     List<string> ReturnList=new List<string>();
   4:     string Location="{0}&sources={1}&appid={2}";
   5:     Location=string.Format(Location,Item,"spell",APPID);
   6:     XmlDocument Doc = new XmlDocument();
   7:     Doc.LoadXml(Search(Location));
   8:     XmlNamespaceManager NamespaceManager = new XmlNamespaceManager(Doc.NameTable);
   9:     NamespaceManager.AddNamespace("api", "");
  10:     NamespaceManager.AddNamespace("spl", "");
  11:     XmlNodeList Nodes = Doc.DocumentElement.SelectNodes("./spl:Spell/spl:Results/spl:SpellResult/spl:Value", NamespaceManager);
  12:     foreach (XmlNode Element in Nodes)
  13:     {
  14:         ReturnList.Add(Element.InnerText);
  15:     }
  16:     return ReturnList;
  17: }

The code above does two things. First it sets up our search string. Turns out that Bing supports a REST like functionality (well, really only GET but whatever). So you set your search string up as a URI and do a call to search:

   1: public string Search(string Location)
   2: {
   3:     try
   4:     {
   5:         HttpWebRequest Request = WebRequest.Create(Location) as HttpWebRequest;
   6:         Request.Method = "GET";
   7:         Request.ContentType = "text/xml";
   8:         using (HttpWebResponse Response = Request.GetResponse() as HttpWebResponse)
   9:         {
  10:             if (Response.StatusCode != HttpStatusCode.OK)
  11:                 throw new Exception("The request did not complete successfully and returned status code " + Response.StatusCode);
  12:             using (StreamReader Reader = new StreamReader(Response.GetResponseStream()))
  13:             {
  14:                 return Reader.ReadToEnd();
  15:             }
  16:         }
  17:     }
  18:     catch { throw; }
  19: }

Search in turn returns the response from Bing, which is an XML file containing our list of suggestions. From there, back in the first function, we simply load it up and parse out the individual spl:Value items and put that in the list to return back. That's all there is to it. The only thing to note is that the search term needs to be encoded but other than that, pretty simple (even that's pretty simple). So take a look, give it a try, and happy coding.


James Craig
January 15, 2010 3:59 PM

Oh, forgot to mention, you also need to get an APP ID from Bing (from here: And you will need to replace the APPID found in the code with what you get from Bing... Other than that, you're good to go.