Se produjo una excepción no controlada del tipo 'System.FormatException' en Microsoft.Speech.dll

I have just tried to use custom pronunciation using a PLS lexicon from thr link My .pls file is as following

Additional information: The lexicon data is invalid or corrupted.

<?xml version="1.0" encoding="UTF-8"?>

<lexicon version="1.0" 
  alphabet="x-microsoft-ups" xml:lang="en-US">

        <grapheme> scale </grapheme>
        <phoneme> S K A L E </phoneme>

and my grammar file is as pronunciation.grxml

<?xml version="1.0" encoding="UTF-8"?>
    xmlns="" tag-format="semantics/1.0" >

  <lexicon uri="C:\Users\sony vaio\Documents\Visual Studio 2012\Projects\ConsoleApplication5\ConsoleApplication5\bin\Debug\Blue.pls" type="application/"/>

  <rule id="colors" scope="public">
        <item> scale </item>


and my c# program is

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using System.Xml;
using Microsoft.Speech;
using Microsoft.Speech.Recognition;
using Microsoft.Speech.Recognition.SrgsGrammar;

namespace ConsoleApplication5
    class Program
        static void Main(string[] args)

            using (SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-US")))

                String currDirPath = Environment.CurrentDirectory;

                string xmlGrammar = currDirPath + "\\pronunciation.grxml";
                string cfgGrammar = currDirPath + "\\pronunciation.cfg";
                FileStream fs = new FileStream(cfgGrammar, FileMode.Create);
                XmlReader reader = XmlReader.Create(xmlGrammar);

                //compile the grammar *.grxml to *.cfg file.
                SrgsGrammarCompiler.Compile(reader, (Stream)fs);
                Grammar g = new Grammar(cfgGrammar, "colors");


                // Add a handler for the speech recognized event.
                recognizer.SpeechRecognized += new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
                // Configure the input to the speech recognizer.

                // Start asynchronous, continuous speech recognition.

                // Keep the console window open.
                while (true)

        // speech recognised event handler

        static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
            string lines = e.Result.Text;
            Console.WriteLine("Speak loudly");
            Console.WriteLine("Recognized text: " + e.Result.Text);
            Console.WriteLine("semantic value: " + e.Result.Semantics.Value.ToString());

but whenever I try to compile it and use it for recognisig "scale" it always shows that "An unhandled exception of type 'System.FormatException' occurred in Microsoft.Speech.dll Additional information: The lexicon data is invalid or corrupted. "

Although when inline pronunciation is used it works but the pronunciation in external link doesn't seems to work. Is there any workaround? Any help would be appreciated. Please.

preguntado el 05 de mayo de 13 a las 12:05

I was not able to reproduce your problem. Although, I did make two edits to the above code and it worked for me. I replaced the using Microsoft.* with System.* and referenced those assemblies instead. I don't have the Microsoft.Speech assemblies installed. But I am not getting the format exception that you are seeing. Can try this against the System.Speech assemblies and see if you still have issues? Also what formats are you saving your grxml and pls files in when you open them in a txt editor? UTF-8 is standard. -

Hey Thanks @MattJohnson. It worked Man. But does that mean the microsoft.* assembly doesn't support this thing and may be this is why I get the system.formatException or lexicon data invlaid or corrupt error. And yes the format I am saving my grxml and pls is in UTF-8 standard. Still I would like to know why not microsoft.* is working? -

No problem. Moving from Microsoft.Speech to System.Speech shouldn't make any difference. My understanding s that they both support the W3C spec and they don't have specific grammar format requirements. -

1 Respuestas

Moving the code above from Microsoft.Speech.Recogonition to System.Speech.Recognition fixes the issue with the format exception above.

As to why Microsoft.Speech.Recognition is throwing the error, I am not sure. I do know that the official W3C spec does officially support a lexicon tag in the xml file (although Microsoft has very specific rules as to where the lexicon tag is supposed to be located this appears to be correct):

Lexicon: The grammar format does not address the loading of lexicons or the pronunciation of words referenced by the grammar. The W3C Voice Browser Working Group is considering the development of a standard lexicon format. If and when a format is developed appropriate updates will be made to this grammar specification.

The actual spec for the lexicon format is not completed yet:

In summary if Microsofts own example linked aquí does not work this appears to be a bug. Especially if all you did to fix the format exception is to change from Microsoft.Speech to System.Speech.

contestado el 07 de mayo de 13 a las 15:05

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.