Grammar Constructor (SrgsDocument)


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.

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

public Grammar(
	SrgsDocument srgsDocument


Type: System.Speech.Recognition.SrgsGrammar.SrgsDocument

The constraints for the speech recognition grammar.

Exception Condition

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.

A SrgsDocument can have a root rule. To create a Grammar object that specifies a root rule, use the Grammar or Grammar constructor.

To create a speech recognition Grammar from an SrgsDocument and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

The following example creates a speech recognition grammar in an SrgsDocument instance, which is then used to construct a Grammar object.

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

  // Create the Cities rule and add it to the document.
  SrgsRule citiesRule = new SrgsRule("Cities");

  SrgsOneOf cityChoice = new SrgsOneOf();
  cityChoice.Add(new SrgsItem("Seattle"));
  cityChoice.Add(new SrgsItem("Los Angeles"));
  cityChoice.Add(new SrgsItem("New York"));
  cityChoice.Add(new SrgsItem("Miami"));


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

  SrgsItem item = new SrgsItem("I would like to fly from");
  item.Add(new SrgsRuleRef(citiesRule));
  item.Add(new SrgsText("to"));
  item.Add(new SrgsRuleRef(citiesRule));


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

  // Create the Grammar object.
  Grammar citiesGrammar = new Grammar(document);
  citiesGrammar.Name = "SrgsDocument Cities Grammar";

  return citiesGrammar;

.NET Framework
Available since 3.0
Return to top