在 c# 中使用 Microsoft Translator 翻译字符串数组
Translate an array of strings using Microsoft Translator in c#
我正在开发一个翻译 xml 文件的 c# 应用程序,我解析了 xml 文件,提取了要翻译的字符串并将它们存储在一个字符串数组中,现在我正在尝试通过 http 发送此字符串数组以使用此进行翻译:
/*string[] translateArraySourceTexts = GetStringArray("./Traduction/xml.xml");*/
public static string[] GetStringArray(string file)
{
XDocument doc = XDocument.Load(file);
var sentences = from l in doc.Descendants("sentence") select (string)l.FirstNode.ToString();
return sentences.ToArray();
}
{ string[] strarr = GetStringArray("./Traduction/xml.xml");
/*循环将 strarr 字符串存储在 translateArraySourceTexts */
for (int i = 0; i < strarr.Count(); i++)
{
string[] translateArraySourceTexts = { strarr[i] };
string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
string body = "<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</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\">{1}</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>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{strarr[i]}</string>" +
"<To>{5}</To>" +
"</TranslateArrayRequest>";
string reqBody = string.Format(body, from, "text/plain", translateArraySourceTexts[i], to);}
//这是我正在使用的 xml 文件 :
<?xml version="1.0" encoding="utf-8"?>
<document source="user">
<sentences>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
</sentences>
</document>
所以我想我的数组必须包含句子“salut tout le monde!” 5次
但我没有得到完整的响应字符串只有 4,而我的数组包含 5
您更改了代码,这令人困惑,但我明白了您的问题。
你必须得到你的元素列表,然后使用你的循环。所以你可以使用:
XDocument doc = XDocument.Load(file);
IEnumerable<XElement> sentences = xmlDoc.Elements("document").Elements("sentences").Elements("sentence");
foreach (XElement s in sentences)
{
string[] translateArraySourceTexts = { strarr[i] };
string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
string body =
"<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</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\">{1}</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>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{2}</string>" +
"<To>{3}</To>" +
"</TranslateArrayRequest>";
string reqBody = string.Format(body, from, "text/plain", s.Value, to);}
}
我正在开发一个翻译 xml 文件的 c# 应用程序,我解析了 xml 文件,提取了要翻译的字符串并将它们存储在一个字符串数组中,现在我正在尝试通过 http 发送此字符串数组以使用此进行翻译:
/*string[] translateArraySourceTexts = GetStringArray("./Traduction/xml.xml");*/
public static string[] GetStringArray(string file)
{
XDocument doc = XDocument.Load(file);
var sentences = from l in doc.Descendants("sentence") select (string)l.FirstNode.ToString();
return sentences.ToArray();
}
{ string[] strarr = GetStringArray("./Traduction/xml.xml");
/*循环将 strarr 字符串存储在 translateArraySourceTexts */
for (int i = 0; i < strarr.Count(); i++)
{
string[] translateArraySourceTexts = { strarr[i] };
string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
string body = "<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</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\">{1}</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>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{strarr[i]}</string>" +
"<To>{5}</To>" +
"</TranslateArrayRequest>";
string reqBody = string.Format(body, from, "text/plain", translateArraySourceTexts[i], to);}
//这是我正在使用的 xml 文件 :
<?xml version="1.0" encoding="utf-8"?>
<document source="user">
<sentences>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
<sentence lang="fr" id="123">
salut tout le monde !
</sentence>
</sentences>
</document>
所以我想我的数组必须包含句子“salut tout le monde!” 5次 但我没有得到完整的响应字符串只有 4,而我的数组包含 5
您更改了代码,这令人困惑,但我明白了您的问题。 你必须得到你的元素列表,然后使用你的循环。所以你可以使用:
XDocument doc = XDocument.Load(file);
IEnumerable<XElement> sentences = xmlDoc.Elements("document").Elements("sentences").Elements("sentence");
foreach (XElement s in sentences)
{
string[] translateArraySourceTexts = { strarr[i] };
string uri = "http://api.microsofttranslator.com/v2/Http.svc/TranslateArray";
string body =
"<TranslateArrayRequest>" +
"<AppId />" +
"<From>{0}</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\">{1}</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>" +
"<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/Arrays\">{2}</string>" +
"<To>{3}</To>" +
"</TranslateArrayRequest>";
string reqBody = string.Format(body, from, "text/plain", s.Value, to);}
}