简体   繁体   中英

API Microsoft Translator Exception: “The data at the root level is invalid. Line 1, position 1.”

I am doing the migration of Microsoft Translation, from v2 to v3. But I am getting the following exception:

There was an error deserializing the object of type System.String. The data at the root level is invalid. Line 1, position 1.
(System.Runtime.Serialization.SerializationException)

Inner exception: The data at the root level is invalid. Line 1, position 1.
System.Exception {System.Xml.XmlException}

This error happens on this line of code:

translation.Append((string)dcs.ReadObject(response));

Full method:

public string TranslateText(string from, string to, string text)
{
        var token = string.Empty;

        try
        {
            retryPolicy.Execute(() =>
            {
                token = azureAuthentication.GetAccessToken();
            });

            var translation = new StringBuilder();
            int charLength = Configuration.Configuration.CharLength;

            List<string> lines = text.Split('.').Aggregate(new[] { "" }.ToList(), (a, x) =>
            {
                var last = a[a.Count - 1];

                if ((last + x).Length > charLength)
                {
                    a.Add(x);
                }
                else
                {
                    a[a.Count - 1] = ($"{last}{x}.");
                }

                return a;
            });
            
            foreach (var str in lines)
            {
                string uri =$"https://api.cognitive.microsofttranslator.com/languages?api-version=3.0&scope=translation&textType={HttpUtility.UrlEncode(str)}&from=from_lang&to=to_lang";
                var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
                httpWebRequest.Headers.Add("Authorization", token);

                try
                {
                    retryPolicy.Execute(() =>
                    {
                        using (var response = httpWebRequest.GetResponse().GetResponseStream())
                        {
                            var dcs = new DataContractSerializer(Type.GetType("System.String"));
                            translation.Append((string)dcs.ReadObject(response));
                        }
                    });

                }
                catch (Exception ex)
                {
                    throw new ExternalServiceUnavailableException(ex.Message, ex);
                }
            }

            return translation.ToString();
        }
        catch(Exception ex)
        {
            throw new ExternalServiceUnavailableException(ex.Message, ex);
        }
 }

I have no idea why this error is occurring. I looked out other questions regarding this error but I am not sure how to implement those resolutions here.

Thank you

I think you're getting an error in your request so the deserialization is failing. I think this part from=from_lang&to=to_lang should be from={from_lang}&to={to_lang}

You're setting to the literal string from_lang, instead of a name of a valid language, like en or es.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM