WriteSrgs Method

Microsoft Speech Platform SDK 11

Collapse imageExpand ImageCopy imageCopyHover image

Writes the contents of the SrgsDocument object to an XML-format grammar file that conforms to the Speech Recognition Grammar Specification (SRGS) Version 1.0.

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

Syntax

Visual Basic (Declaration)
Public Sub WriteSrgs ( _
	srgsGrammar As XmlWriter _
)
Visual Basic (Usage)
Dim instance As SrgsDocument
Dim srgsGrammar As XmlWriter

instance.WriteSrgs(srgsGrammar)
C#
public void WriteSrgs(
	XmlWriter srgsGrammar
)

Parameters

srgsGrammar
Type: System.Xml..::..XmlWriter

The XmlWriter that is used to write the SrgsDocument instance.

Exceptions

ExceptionCondition
ArgumentNullException

srgsGrammar is nullNothingnullptrunita null reference (Nothing in Visual Basic).

Examples

The following example creates an SrgsDocument object, and then creates a public rule named winnerRule. It then creates an SrgsItem that consists of the string "A nation that has won the world cup is:", and adds this item to the Elements property of the rule. The example then creates two more rules (ruleEurope and ruleSAmerica), each of which is an SrgsOneOf object that contains three SrgsItem objects. After that, another SrgsOneOf object is created that contains SrgsRuleRef objects that refer to ruleEurope and ruleSAmerica. The new SrgsOneOf object is then added to the Elements property of winnerRule. After this, all three rules (winnerRule, ruleEurope, and ruleSAmerica) are added to the Rules property of the SrgsDocument. Finally, the example creates an empty XML file and an instance of XmlWriter. The WriteSrgs method uses the XmlWriter instance to write the contents of the SrgsDocument to the XML file.

C# Copy imageCopy Code
public void WorldSoccerWinners ()
{

  // Create an SrgsDocument, create a new rule
  // and set its scope to public.
  SrgsDocument document = new SrgsDocument();
  SrgsRule winnerRule = new SrgsRule("WorldCupWinner");
  winnerRule.Scope = SrgsRuleScope.Public;

  // Add the introduction.
  winnerRule.Elements.Add(new SrgsItem("A nation that has won the world cup is: "));

  // Create the rule for the European nations.
  SrgsOneOf oneOfEurope = new SrgsOneOf(new SrgsItem[] {new SrgsItem("England"), 
    new SrgsItem("France"), new SrgsItem("Germany"), new SrgsItem("Italy")});
  SrgsRule ruleEurope = (new SrgsRule("EuropeanNations", new SrgsElement[] {oneOfEurope}));

  // Create the rule for the South American nations.
  SrgsOneOf oneOfSAmerica = new SrgsOneOf(new SrgsItem[] {new SrgsItem("Argentina"), 
    new SrgsItem("Brazil"), new SrgsItem("Uruguay")});
  SrgsRule ruleSAmerica = (new SrgsRule("SouthAmericanNations", new SrgsElement[] {oneOfSAmerica}));

  // Add references to winnerRule for ruleEurope and ruleSAmerica.
  winnerRule.Elements.Add(new SrgsOneOf(new SrgsItem[] {(new SrgsItem 
    (new SrgsRuleRef(ruleEurope))), new SrgsItem(new SrgsRuleRef(ruleSAmerica))}));

  // Add all the rules to the document and make winnerRule 
  // the root rule of the document.
  document.Rules.Add(new SrgsRule[] {winnerRule, ruleEurope, ruleSAmerica});
  document.Root = winnerRule;

  // Create a string object with the path to the file to use.
  string srgsDocumentFile = Path.Combine(Path.GetTempPath(), "srgsDocumentFile.xml");

  // Create an XmlWriter object and pass the file path.
  XmlWriter writer = XmlWriter.Create(srgsDocumentFile);

  // Write the contents of the XmlWriter object to an SRGS-compatible XML file.
  document.WriteSrgs(writer);
  writer.Close();
}

See Also