SrgsDocument.WriteSrgs(XmlWriter) 方法

定义

SrgsDocument 对象的内容写入符合语音识别语法规范 (SRGS) 1.0 版的 XML 格式语法文件。

public:
 void WriteSrgs(System::Xml::XmlWriter ^ srgsGrammar);
public void WriteSrgs (System.Xml.XmlWriter srgsGrammar);
member this.WriteSrgs : System.Xml.XmlWriter -> unit
Public Sub WriteSrgs (srgsGrammar As XmlWriter)

参数

srgsGrammar
XmlWriter

用于编写 XmlWriter 实例的 SrgsDocument

例外

srgsGrammarnull

示例

以下示例创建一个 SrgsDocument 对象,然后创建一个名为 winnerRule的公共规则。 然后,它创建一个由 SrgsItem 字符串“赢得世界杯的国家是:”组成的 ,并将此项添加到 Elements 规则的 属性。 然后,该示例 (ruleEuroperuleSAmerica) 再创建两个规则,每个规则都是一个包含三SrgsItemSrgsOneOf对象的 对象。 之后,将创建另一个 SrgsOneOf 对象,其中包含 SrgsRuleRef 引用 ruleEuroperuleSAmerica的对象。 然后将新 SrgsOneOf 对象添加到 ElementswinnerRule属性。 之后, (winnerRuleruleEuroperuleSAmerica) 的所有三个规则都会添加到 Rules 的 属性中 SrgsDocument。 最后,该示例创建一个空的 XML 文件和 一个 实例 XmlWriterWriteSrgs方法使用 XmlWriter 实例将 的内容SrgsDocument写入 XML 文件。

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();
}

适用于