Export (0) Print
Expand All

SrgsText Class

Represents the textual content of grammar elements defined by the World Wide Web Consortium (W3C) Speech Recognition Grammar Specification (SRGS) Version 1.0.

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

public sealed class SrgsText : SrgsElement

The SrgsText class represents the text found within a set of SRGS element tags. When an SrgsItem object is constructed with a String parameter, a SrgsText object is created with its Text property initialized to the value of that parameter. The Text object is then added to the Elements collection on the SrgsItem object.

The following C# code example demonstrates how to use the SrgsText class to modify the textual contents of SrgsItem objects. The example changes the initial text values of the SrgsItem objects (Large, Larger, and Largest) to Small, Medium, and Large, respectively.

// Create SrgsItem objects and specify their text.
SrgsItem smallItem = new SrgsItem("Large");
SrgsItem mediumItem = new SrgsItem("Larger");
SrgsItem largeItem = new SrgsItem("Largest");

SrgsText textOfItem = null;

//  Change the text of smallItem.
if (smallItem.Elements[0] is SrgsText)
  textOfItem = smallItem.Elements[0] as SrgsText;
  textOfItem.Text = "Small";

//  Change the text of mediumItem.
if (mediumItem.Elements[0] is SrgsText)
  textOfItem = mediumItem.Elements[0] as SrgsText;
  textOfItem.Text = "Medium";

// Change the text of largeItem.
if (largeItem.Elements[0] is SrgsText)
  textOfItem = largeItem.Elements[0] as SrgsText;
  textOfItem.Text = "Large";

// Create an SrgsOneOf object and add smallItem, mediumItem, 
// and largeItem as alternatives.
SrgsOneOf itemSize = new SrgsOneOf(new SrgsItem[] 
  { smallItem, mediumItem, largeItem });

// Create a new SrgsRule from the SrgsOneOf object, and specifiy its identifier.
SrgsRule size = new SrgsRule("Sizes", itemSize);

// Create an SrgsDocument object.
// Add the SrgsRule object to the collection of rules and make it the root rule.
SrgsDocument document = new SrgsDocument(); 
document.Root = size;

// Write the SrgsDocument to an XML grammar file.
string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");
XmlWriter writer = XmlWriter.Create(srgsDocumentFile);

The following shows how the modified text of the SrgsItem objects would appear as item elements in the output XML grammar file.

<!-- SRGS XML Fragment -->

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
© 2014 Microsoft