Export (0) Print
Expand All

Grammar Constructor (Stream)

Initializes a new instance of the Grammar class from a Stream.

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

public Grammar(
	Stream stream
)

Parameters

stream
Type: System.IO.Stream

A stream that describes a speech recognition grammar in a supported format.

ExceptionCondition
ArgumentException

stream describes a grammar that does not contain a root rule.

ArgumentNullException

stream is null.

FormatException

The stream does not contain a valid description of a grammar, or describes a grammar that contains a rule reference that cannot be resolved.

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

This constructor can create a Grammar instance from the following formats:

This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a Grammar object from an XML-format grammar by compiling the grammar in advance, using one of the Compile methods.

An SRGS grammar can define a root rule. To create a Grammar object from a stream and specify a root rule, use the Grammar or Grammar constructor.

To create a Grammar object from a stream and specify a base URI to use to resolve relative rule references, use the Grammar constructor.

The following example creates a speech recognition grammar from a local SRGS file (cities.xml) using a file stream. The content of the cities.xml file appears following C# example.

// Load a cities grammar from an I/O stream and  
// return the new grammar. 
private static Grammar CreateGrammarFromStream()
{
  string fileName = @"c:\temp\cities.xml";
  Grammar citiesGrammar =
    new Grammar(new FileStream(fileName, FileMode.Open));
  citiesGrammar.Name = "Stream Cities Grammar";
  return citiesGrammar;
}
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
         xmlns="http://www.w3.org/2001/06/grammar"
         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">
    <item>
      I would like to fly from <ruleref uri="#Cities"/>
      to <ruleref uri="#Cities"/>
    </item>
  </rule>

  <rule id="Cities" scope="public">
    <one-of>
      <item>Seattle</item>
      <item>Los Angeles</item>
      <item>New York</item>
      <item>Miami</item>
    </one-of>
  </rule>
</grammar>

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft