public void TranslateArray(List<string> textsToTranslate, string languageFrom, string languageTo, List<string> keys, TranslateCompleteArrayCallback callbackMethod) { string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray"; //Generate the request body string body = "<TranslateArrayRequest>" + "<AppId />" + "<From>"; body += languageFrom; body += "</From>" + "<Options>" + " <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">text/plain</ContentType>" + "<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "</Options>" + "<Texts>"; foreach(string text in textsToTranslate) { body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">" + text + "</string>"; } body += "</Texts>" +"<To>"; body += languageTo; body += "</To>" + "</TranslateArrayRequest>"; // create the request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Headers.Add("Authorization", headerValue); request.ContentType = "text/xml"; request.Method = "POST"; using (System.IO.Stream stream = request.GetRequestStream()) { byte[] arrBytes = System.Text.Encoding.UTF8.GetBytes(body); stream.Write(arrBytes, 0, arrBytes.Length); } // Get the response WebResponse response = null; try { response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { List<string> translatedTexts = new List<string>(); XmlReader reader = XmlReader.Create(stream); while (reader.Read()) { if(reader.NodeType == XmlNodeType.Element) { if (reader.Name == "TranslateArrayResponse") { if (reader.ReadToDescendant("TranslatedText")) { do { translatedTexts.Add(reader.ReadString()); } while (reader.ReadToNextSibling("TranslatedText")); } } } } reader.Close(); if(callbackMethod != null) { callbackMethod(keys, translatedTexts); } callbackMethod = null; keys.Clear(); translatedTexts.Clear(); } } catch { throw; } finally { if (response != null) { response.Close(); response = null; } } }
public void TranslateArray(List <string> textsToTranslate, string languageFrom, string languageTo, List <string> keys, TranslateCompleteArrayCallback callbackMethod) { string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray"; //Generate the request body string body = "<TranslateArrayRequest>" + "<AppId />" + "<From>"; body += languageFrom; body += "</From>" + "<Options>" + " <Category xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<ContentType xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\">text/plain</ContentType>" + "<ReservedFlags xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<State xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<Uri xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "<User xmlns=\"http://schemas.datacontract.org/2004/07/Microsoft.MT.Web.Service.V2\" />" + "</Options>" + "<Texts>"; foreach (string text in textsToTranslate) { body += "<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">" + text + "</string>"; } body += "</Texts>" + "<To>"; body += languageTo; body += "</To>" + "</TranslateArrayRequest>"; // create the request HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri); request.Headers.Add("Authorization", headerValue); request.ContentType = "text/xml"; request.Method = "POST"; using (System.IO.Stream stream = request.GetRequestStream()) { byte[] arrBytes = System.Text.Encoding.UTF8.GetBytes(body); stream.Write(arrBytes, 0, arrBytes.Length); } // Get the response WebResponse response = null; try { response = request.GetResponse(); using (Stream stream = response.GetResponseStream()) { List <string> translatedTexts = new List <string>(); XmlReader reader = XmlReader.Create(stream); while (reader.Read()) { if (reader.NodeType == XmlNodeType.Element) { if (reader.Name == "TranslateArrayResponse") { if (reader.ReadToDescendant("TranslatedText")) { do { translatedTexts.Add(reader.ReadString()); }while (reader.ReadToNextSibling("TranslatedText")); } } } } reader.Close(); if (callbackMethod != null) { callbackMethod(keys, translatedTexts); } callbackMethod = null; keys.Clear(); translatedTexts.Clear(); } } catch { throw; } finally { if (response != null) { response.Close(); response = null; } } }