我正在研究转换xml文件的ac#app,我解析了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");
/ *循环在translateArraySourceTexts * /
中存储strarr字符串 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
答案 0 :(得分:0)
您更改了代码,令人困惑但我看到了您的问题。 你必须得到你的元素列表,然后使用你的循环。所以你可以使用:
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);}
}