共用方式為


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 字串,其中包含「已贏得 World Cup 為:」的字串,並將這個專案新增至 Elements 規則的 屬性。 然後,此範例會建立兩個規則 (ruleEuroperuleSAmerica) ,每個規則都是包含三 SrgsItemSrgsOneOf 物件的 物件。 之後,會建立另一個 SrgsOneOf 物件,其中包含 SrgsRuleRef 參考 ruleEuroperuleSAmerica 的物件。 然後將新的 SrgsOneOf 物件新增至 ElementswinnerRule 屬性。 在此之後, (、 ruleEurope 和) 的所有三個規則 winnerRule 都會新增至 RulesSrgsDocumentruleSAmerica 屬性。 最後,此範例會建立空的 XML 檔案和 的 XmlWriter 實例。 方法 WriteSrgsXmlWriter 使用 實例,將 的內容 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();
}

適用於