Grammar Constructor (SrgsDocument, String)

Constructs a new instance of Grammar from an instance of SrgsDocument the name of a particular rule to be the grammar’s root rule may also be specified.

Namespace: Microsoft.Speech.Recognition
Assembly: Microsoft.Speech (in microsoft.speech.dll)




  • srgsDocument
    An instance of SrgsDocument to a file containing a rules and a grammar to be used to construct the Grammar object.
  • ruleName
    Name of a grammar rule used as the entry point or root of the Grammar object to be created.

    This parameter may be null.


Exception type Condition

Generated for invalid srgsDocuments, and ruleName.

Generated if the SrgsDocument specified by srgsDocument does not contain the rule ruleName, or if ruleName is null and srgsDocument does not have a root rule..


The srgsDocument argument

  • Must never be null.

  • Must contain the rule specified by ruleName, if ruleName is not null, and a root rule if ruleName is null.

The ruleName argument:

  • May be null or System.String.Empty.

  • If ruleName is not null and the rule specified is not found in the grammar being loaded, an exception is generated.

  • If ruleName is null, the grammar contained in the file specified by must specify a root rule. If there is not root rule in the grammar being loaded an exception is generated.

    (For more information on root rules, see the discussion of the Root property on the Microsoft.Speech.Recognition.SrgsGrammar.SrgsDocument class.)

No parameters are supplied to this constructor, therefore the loaded rule should either have no initialization handler defined, or the initialization handler should require no arguments.

As there is no Base URI specified, any relative rule references must:

  • Have with absolute URIs.

  • Be contained within the grammar being loaded.

  • Using any paths defined in the grammar object being loaded. If the stream was created from a file, this typically includes the directory where that file was located.


The following try/catch block attempts to create a new Grammar from an instance of SrgsDocument (srgsDoc).

The SrgsDocument instance must:

  • Contain a rule named "MapLookup"

  • Have all references to other rules and grammars accessible in default locations.

  • Have no initialization handler, or a handler that takes not arguments.

XML defining SRGS content that would satisfy these conditions can be found following the source to this try/catch block.

try {
  Grammar grammar = new Grammar(srgsDoc,
  grammar.Enabled = true;

 } catch (System.ArgumentException exp) {
  MessageBox.Show(String.Format("Invalid constructor arguments:\n{0}\n", exp.Message));
 } catch (Exception exp) {
  // Catch all other exceptions
  MessageBox.Show(String.Format("Failed to load {0}.\nError Message:\n{1}",
            filename, exp.Message));

A sample MapLookup.xml:

<grammar xml:lang="en-us" version="1.0"
xmlns="" tag-format="properties-ms/1.0"
sapi:debug="True" root="MapLookup">
  <rule id="MapLookup" scope="public" sapi:onParse="OnParseMapLookup" sapi:onInit="OnInit" >
   <sapi:script sapi:rule="MapLookup">
       public object OnParseMapLookup(SemanticValue result, RecognizedWordUnit[] words) {
    Console.WriteLine("OnParse MapLookup: " + _value);
    return _value;

public SrgsRule[] OnInit(string state_region, string city, string country) {
    Console.WriteLine(String.Format("Initializer using three arguments: {0},{1},{2}\n",
                                     state_region, city, country));
    _state_region = state_region;
    _city = city;
    _country = country;
    return null;

public SrgsRule[] OnInit( string city, string state_region)
   Console.WriteLine (String.Format("Initializer using two arguments: {0},{1}\n",
    _state_region = state_region;
    _country="United States";
    return null;

public SrgsRule[] OnInit(string country) {
    Console.WriteLine(String.Format("Initializer using one argument: {0}\n",
    _state_region = null;
    _city = null;
    _country = country;
    return null;

public SrgsRule[] OnInit() {
    Console.WriteLine(String.Format("Initializer using no arguments\n"));
    _state_region = null;
    _city = null;
    _country = "United States";
    return null;

private string _state_region;
private string _city;
private string _state_region1;
private string _city1;
private string _value;
private string _country;



Development Platforms

Windows XP Professional with Service Pack 2 (SP2), Windows Server 2003, Windows Vista Ultimate Edition, Windows Vista Business Edition, Windows Vista Enterprise Edition

Target Platforms

See Also


Grammar Class
Grammar Members
Microsoft.Speech.Recognition Namespace
Grammar Constructor

Other Resources

Speech Recognition Grammar Specification