SrgsDocument Constructor (SrgsRule)


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 the SrgsDocument class and specifies an SrgsRule object to be the root rule of the grammar.

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

public SrgsDocument(
	SrgsRule grammarRootRule


Type: System.Speech.Recognition.SrgsGrammar.SrgsRule

The root rule in the SrgsDocument object.

Exception Condition

grammarRootRule is null.

This constructor adds the specified rule to the SrgsRulesCollection of the SrgsDocument object and sets it as the Root rule for the grammar.

The following example creates two rules (chooseCities and destCities) for choosing origin and destination cities for a flight. The example initializes the SrgsDocument instance using the chooseCities rule as an argument. The example writes the contents of the rules collection and the name of the root rule to the console.

// Create a rule that contains a list of destination cities.
SrgsRule destCities = new SrgsRule("Destination");
SrgsOneOf toCities = new SrgsOneOf(new string[] { "New York", "Seattle", "Denver" });

// Create a list of origin cities and supporting phrases.
SrgsOneOf fromCities = new SrgsOneOf(new SrgsItem[] { 
  new SrgsItem("Dallas"), new SrgsItem("Miami"), new SrgsItem("Chicago") });
SrgsItem intro = new SrgsItem("I want to fly from");
SrgsItem to = new SrgsItem("to");

// Create the root rule of the grammar, and assemble the components.
SrgsRule chooseCities = new SrgsRule("Trip");
chooseCities.Add(new SrgsRuleRef(destCities));

// Create the SrgsDocument and specify the root rule to add.
SrgsDocument bookFlight = new SrgsDocument(chooseCities);

// Add the rule for the destination cities to the document's rule collection.
bookFlight.Rules.Add(new SrgsRule[] { destCities });

// Display the contents of the Rules collection and the name of the root rule.
foreach (SrgsRule rule in bookFlight.Rules)
  Console.WriteLine("Rule " + rule.Id + " is in the rules collection");
Console.WriteLine("Root Rule " + bookFlight.Root.Id);

// Create a Grammar object and load it to the recognizer.
Grammar g = new Grammar(bookFlight);
g.Name = ("City Chooser");

.NET Framework
Available since 3.0
Return to top