일단 Create an AppID 만들기
http://www.bing.com/developers/createapp.aspx
우선 지역선택에서 미쿡으로 지정
Bing API SDK 다운로드 및 소개
http://kojaedoo.tistory.com/444Bing API안에 있는
Microsoft Translator API의 간단한 설명
http://msdn.microsoft.com/en-us/library/ff512423(v=MSDN.10).aspx
나중에 실버라이트에도 적용하기 위해 HTTP Service 를 이용 ㅋ
아래는 구동되는 코드
번역 눌렀을때
소스코드
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);
}
}
}
}
'C#.NET' 카테고리의 다른 글
| [PropertyInfo] 임의의 클래스에서 프로퍼티(Property) /메소드(Method) 찾기 (0) | 2010.07.09 |
|---|---|
| [VS 2005,비주얼 스튜디오] 잘못된 바인딩 핸들입니다. (0) | 2010.06.01 |
| [Application/ Application 기본정보 알기] 응용프로그램의 시작위치를 알고 싶을떄? (0) | 2010.05.27 |
| [#if/조건부 지시문] 디버그모드 릴리즈모드 사용하기 (0) | 2010.05.24 |
| [AssemblyName] 파일이 어셈블리인지 확인 / 외부어셈블리 정보 확인 (0) | 2010.05.20 |
| [BindingList 추가/삭제/수정 ] Update Insert Delete 한꺼번에 처리하기 (0) | 2010.04.08 |
| 엑셀출력시 0 사라짐 (0) | 2010.04.08 |
| enum 이름 가져오기 와 리소스(Resources)사용법 (0) | 2010.04.02 |
| [MSDN] EventHandler (0) | 2010.03.31 |
| [C#]사용자 아이피 알아오기 (0) | 2010.03.17 |