일단 Create an AppID 만들기

http://www.bing.com/developers/createapp.aspx

우선 지역선택에서 미쿡으로 지정

image

 

image

 

image

 

Bing API SDK 다운로드 및 소개

http://kojaedoo.tistory.com/444

 

Bing API안에 있는

Microsoft Translator  API의 간단한  설명

http://msdn.microsoft.com/en-us/library/ff512423(v=MSDN.10).aspx

image

나중에 실버라이트에도 적용하기 위해 HTTP Service 를 이용 ㅋ

 

 

아래는 구동되는 코드

image

번역 눌렀을때

image

 

소스코드

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Xml;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        const string AppId = "위에보고 키 발급받아요!~ ㅋ";

        public Form1()
        {
            InitializeComponent();                     
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
            this.comboBox2.SelectedIndex = 1;

        }

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

                // Common request fields (required)
                + "AppId=" + AppId
                + "&Query=" + str
                + "&Sources=Translation"

                // Common request fields (optional)
                + "&Version=2.2"

                // SourceType-specific request fields (required)
                + "&Translation.SourceLanguage=" + SourceLanguage
                + "&Translation.TargetLanguage=" + TargetLanguage;

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

            return request;
        }

    static string 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);
        string result = string.Empty;
        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
            // Translation results.
            result =   DisplayResults(document.DocumentElement, nsmgr);
        }
        return result;
    }


    static string DisplayResults(XmlNode root, XmlNamespaceManager nsmgr)
    {
        string version = root.SelectSingleNode("./@Version", nsmgr).InnerText;
        string searchTerms = 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");

        XmlNodeList results = root.SelectNodes(
            "./tra:Translation/tra:Results/tra:TranslationResult",
            nsmgr);

        // Display the Translation results.
        string MsgResult = string.Empty;
        foreach (XmlNode result in results)
         {

             MsgResult= result.SelectSingleNode("./tra:TranslatedTerm", nsmgr).InnerText;
         }
        return MsgResult;
    }
    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();
        }
    }

    private void button1_Click_1(object sender, EventArgs e)
    {

        HttpWebRequest request = BuildRequest(this.textBox1.Text , this.comboBox1.SelectedItem.ToString() , this.comboBox2.SelectedItem.ToString());
        try
        {
            // Send the request; display the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            this.textBox2.Text =  DisplayResponse(response);
        }
        catch (WebException ex)
        {
            // An exception occurred while accessing the network.
            Console.WriteLine(ex.Message);
        }
    }

    }
}

+ Recent posts