Grammar Constructor (SrgsDocument, String, Uri)


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Initializes a new instance of a Grammar class from an SrgsDocument object, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.

Namespace:   System.Speech.Recognition
Assembly:  System.Speech (in System.Speech.dll)

public Grammar(
	SrgsDocument srgsDocument,
	string ruleName,
	Uri baseUri


Type: System.Speech.Recognition.SrgsGrammar.SrgsDocument

The constraints for the speech recognition grammar.

Type: System.String

The identifier of the rule to use as the entry point of the speech recognition grammar, or null to use the default root rule of the SrgsDocument.

Type: System.Uri

The base URI to use to resolve any relative rule reference in the SrgsDocument, or null.

Exception Condition

ruleName cannot be resolved or is not public, or ruleName is null and srgsDocument does not contain a root rule.


srgsDocument is null.


srgsDocument contains a rule reference that cannot be resolved.

This constructor does not pass any parameters to the initialization handler, and the SrgsDocument should not contain an initialization handler that requires arguments.

This constructor does not validate baseUri. However, the LoadGrammar method of a SpeechRecognitionEngine or SpeechRecognizer object throws an exception if it cannot resolve all of the rule references in the grammar description. If baseUri is not null, the LoadGrammar method uses the URI to resolve any rule references that it cannot otherwise resolve. If baseUri represents a file, then the LoadGrammar method uses both the designated file and the file's directory when it attempts to resolve relative rule references.

The following example creates a speech recognition grammar in an SrgsDocument that contains a relative rule reference to the cities.xml file, and specifies a URI to use to resolve the rule reference. The content of the cities.xml file appears in the XML example that follows the C# example.

private static Grammar CreateSrgsDocumentGrammar3()
  // Create the SrgsDocument.
  SrgsDocument document = new SrgsDocument();

  // Create the Main rule and add it to the document.
  SrgsRule mainRule = new SrgsRule("Main");
  mainRule.Scope = SrgsRuleScope.Public;

  SrgsItem item = new SrgsItem("Can I get a shuttle in");

  // Create a relative URI for the cities rule.
  Uri ruleUri = new Uri("cities.xml#Cities", UriKind.Relative);

  item.Add(new SrgsRuleRef(ruleUri));


  // Set the root rule.
  document.Root = mainRule;

  // Create the grammar.
  Uri baseUri = new Uri(@"file://c:\temp\");
  Grammar citiesGrammar = new Grammar(document, null, baseUri);
  citiesGrammar.Name = "SrgsDocument Cities Grammar 3";

  return citiesGrammar;

<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
         tag-format="semantics/1.0" root="Main">

  <!-- cities.xml: 
    Defines an SRGS grammar for requesting a flight. This grammar includes
    a Cities rule that lists the cities that can be used for departures
    and destinations. -->

  <rule id="Main">
      I would like to fly from <ruleref uri="#Cities"/>
      to <ruleref uri="#Cities"/>

  <rule id="Cities" scope="public">
      <item>Los Angeles</item>
      <item>New York</item>

.NET Framework
Available since 3.0
Return to top