語言

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用 the 來撰寫實SrgsDocument例。

例外狀況

srgsGrammar 是 null。

範例

以下範例建立一個 SrgsDocument 物件,然後建立一個名為 winnerRule的公開規則。 接著它會產生 SrgsItem 一個由字串「A nation who had won the World Cup」組成的 ,並將此項目加入 Elements 規則的屬性中。 接著範例會產生兩條規則(ruleEurope 和 ruleSAmerica),每個規則都包含 SrgsOneOf 三個 SrgsItem 物件。 之後,會建立另一個 SrgsOneOf 物件,包含 SrgsRuleRef 指向 ruleEurope 和 ruleSAmerica的物件。 接著將新 SrgsOneOf 物件加入 Elements 的 winnerRule性質中。 之後,將三條規則winnerRule(、 ruleEurope、 ruleSAmerica和 )全部加入 的屬性Rules中SrgsDocument。 最後,範例會建立一個空的 XML 檔案和一個 的 XmlWriter實例。 此 WriteSrgs 方法利用 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();
}

適用於