0

I have a verb I need to read in spanish. However, the Twilio .Net parser is failing with Invalid character in the given encoding. The line that's failing (edited for brevity) is:

> <Say voice="man" language="es">cita para la instalación de alfombra,
> el lunes, 15 de enero</Say>

My XML is defined as <?xml version="1.0" encoding="utf-8"?>

How can I make the Twilio .Net library parse that character correctly? If I put this directly in a TWIML bin it works, however, the .Net SDK doesn't parse it.

Updated: Adding code examples

I'm using the helper library to generate VoiceResponse objects and returning them through the webhook. For example:

foreach (var (translation, date) in installations)
{
    gather.Append(new Say($"{translation} {SpanishDate(date)}", Say.VoiceEnum.Man, language: "es"));
}
public static string SpanishDate(DateTime date)
{
return $"el {date.ToString("dddd", new CultureInfo("es-ES"))}, {(date.Day == 1 ? "primo" : date.Day.ToString())} de {date.ToString("MMMM", new CultureInfo("es-ES"))}";
}

I'm looking at the resulting XML that gets returned by the helper library, which is:

<Say voice="man" language="es">Cita para la instalacion de pisos de madera  el miércoles, 28 de febrero</Say>

The XML that's generated by the VoiceResponse object (using the ToString() method) is stored in blob storage until the call is placed, then returned as a TwiMLResult in the webhook.

The exception is being thrown in the helper library's TwiMLResult constructor, which is attempting to parse the XML and is failing.

at System.Xml.XmlTextReaderImpl.Throw(Exception e) 
at System.Xml.XmlTextReaderImpl.InvalidCharRecovery(Int32& bytesCount, Int32& charsCount) 
at System.Xml.XmlTextReaderImpl.GetChars(Int32 maxCharsCount) 
at System.Xml.XmlTextReaderImpl.ReadData() 
at System.Xml.XmlTextReaderImpl.ParseText(Int32& startPos, Int32& endPos, Int32& outOrChars) 
at System.Xml.XmlTextReaderImpl.FinishPartialValue() 
at System.Xml.XmlTextReaderImpl.get_Value() 
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r) 
at System.Xml.Linq.XContainer.ReadContentFrom(XmlReader r, LoadOptions o) 
at System.Xml.Linq.XDocument.Load(XmlReader reader, LoadOptions options) 
at Twilio.AspNet.Mvc.TwiMLResult.LoadFromString(String twiml, Encoding encoding) 
at Twilio.AspNet.Mvc.TwiMLResult..ctor(String twiml) 

2 Answers2

1

After looking at how @Marcos did his sample, I found that when I return the pre-rendered XML in the TwiMLResult, I wasn't setting the encoding there to UTF8. The proper way to do it is (example):

var xml = voiceResponse.ToString();
var result = new TwiMLResult(xml, Encoding.UTF8);
0

Twilio developer evangelist here.

Looking at the example you posted, it doesn't look like you are generating TwiML by using the helper library you can find here. You can install it by running:

Install-Package Twilio

I wrote a sample application using the same string as you are and got the correct XML to be generated.

public IActionResult Index()
{       
    var response = new VoiceResponse();
    response.Say("cita para la instalación de alfombra, el lunes, 15 de enero!", language: "es");

    return Content(response.ToString(), "text/xml");
}

The above returns:

<?xml version="1.0" encoding="utf-8"?>
<Response>
  <Say language="es">cita para la instalación de alfombra, el lunes, 15 de enero!</Say>
</Response>

Hope this helps you.

Marcos Placona
  • 21,468
  • 11
  • 68
  • 93
  • I updated my question to show how I'm using the helper library, your example doesn't use the TwiML result object, which I think is the culprit of the exception. – Steven Creaney Feb 20 '18 at 18:48