-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathGrammar.xml
More file actions
1996 lines (1720 loc) · 121 KB
/
Copy pathGrammar.xml
File metadata and controls
1996 lines (1720 loc) · 121 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<Type Name="Grammar" FullName="System.Speech.Recognition.Grammar">
<TypeSignature Language="C#" Value="public class Grammar" />
<TypeSignature Language="ILAsm" Value=".class public auto ansi beforefieldinit Grammar extends System.Object" />
<TypeSignature Language="DocId" Value="T:System.Speech.Recognition.Grammar" />
<TypeSignature Language="VB.NET" Value="Public Class Grammar" />
<TypeSignature Language="F#" Value="type Grammar = class" />
<TypeSignature Language="C++ CLI" Value="public ref class Grammar" />
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>8.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.0</AssemblyVersion>
<AssemblyVersion>9.0.0.1</AssemblyVersion>
<AssemblyVersion>9.0.0.2</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Base>
<BaseTypeName>System.Object</BaseTypeName>
</Base>
<Interfaces />
<Attributes>
<Attribute FrameworkAlternate="netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.Diagnostics.DebuggerDisplay("Grammar: {(_uri != null ? "uri=" + _uri.ToString () + " " : "") + "rule=" + _ruleName }")]</AttributeName>
<AttributeName Language="F#">[<System.Diagnostics.DebuggerDisplay("Grammar: {(_uri != null ? "uri=" + _uri.ToString () + " " : "") + "rule=" + _ruleName }")>]</AttributeName>
</Attribute>
</Attributes>
<Docs>
<summary>A runtime object that references a speech recognition grammar, which an application can use to define the constraints for speech recognition.</summary>
<remarks>
<format type="text/markdown"><), and [Create Grammars Using SrgsGrammar](https://learn.microsoft.com/previous-versions/office/developer/speech-technologies/hh361680(v%3doffice.14)).
After you author a grammar, you must build it into a <xref:System.Speech.Recognition.Grammar> object that a speech recognition engine can load and that your application can use at runtime to manage speech recognition. You can use a <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor to create a <xref:System.Speech.Recognition.Grammar> instance from a <xref:System.Speech.Recognition.GrammarBuilder> or a <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> object, or from a file or a <xref:System.IO.Stream> that contains a description of a grammar in a supported format. Supported formats include the following:
- XML-format files that conform to the W3C [Speech Recognition Grammar Specification (SRGS) Version 1.0](https://go.microsoft.com/fwlink/?LinkId=201761)
- Grammars that have been compiled to a binary file with a .cfg file extension
Grammar constructors that accept XML-format grammar files in their arguments compile the XML grammars to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
An application's speech recognition engine, as managed by a <xref:System.Speech.Recognition.SpeechRecognizer> or <xref:System.Speech.Recognition.SpeechRecognitionEngine> object, can load multiple speech recognition grammars. The application can independently enable or disable individual grammars by setting the <xref:System.Speech.Recognition.Grammar.Enabled%2A> property, and modify recognition behavior through <xref:System.Speech.Recognition.Grammar> properties, such as the <xref:System.Speech.Recognition.Grammar.Priority%2A> and <xref:System.Speech.Recognition.Grammar.Weight%2A> properties.
The grammar's <xref:System.Speech.Recognition.Grammar.SpeechRecognized> event is raised when input matches a path through the grammar.
[!INCLUDE [untrusted-data-instance-note](~/includes/untrusted-data-instance-note.md)]
> [!NOTE]
> It is a best practice to verify the safety of any URI or DLL used to build a <xref:System.Speech.Recognition.Grammar> object, which helps prevent security vulnerabilities when loading external resources.
>
> Windows and the Speech platform provide security for applications constructing a <xref:System.Speech.Recognition.Grammar> instance from a DLL or from a grammar that supports scripting.
>
> Scripts in <xref:System.Speech.Recognition.Grammar> objects are always run as if downloaded from a web page in the `Internet Zone`. The Common Language Runtime (CLR) isolates any DLL loaded to obtain a grammar definition.
## Examples
The following example constructs a <xref:System.Speech.Recognition.Grammar> object from a speech recognition grammar defined in a XML file (cities.xml). The content of the cities.xml file appears in the following XML example.
```csharp
// Load a cities grammar from a local file and return the grammar object.
private static Grammar CreateGrammarFromFile()
{
Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml");
citiesGrammar.Name = "SRGS File Cities Grammar";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->
<rule id="Main">
<item>
I would like to fly from <ruleref uri="#Cities"/>
to <ruleref uri="#Cities"/>
</item>
</rule>
<rule id="Cities" scope="public">
<one-of>
<item> Seattle </item>
<item> Los Angeles </item>
<item> New York </item>
<item> Miami </item>
</one-of>
</rule>
</grammar>
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" />
<altmember cref="T:System.Speech.Recognition.GrammarBuilder" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
<Members>
<MemberGroup MemberName=".ctor">
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class.</summary>
<remarks>
<format type="text/markdown"><
- Grammars that have been compiled to a binary file with a .cfg file extension
Grammar constructors that accept XML-format grammar files in their arguments compile the XML grammars to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
A speech recognition grammar can define a root rule. To create a <xref:System.Speech.Recognition.Grammar> object that specifies which rule to use as its root rule, use a constructor that accepts the `ruleName` parameter.
To create a <xref:System.Speech.Recognition.Grammar> object that specifies a base URI to resolve relative rule references, use a constructor that takes the `baseUri` parameter.
]]></format>
</remarks>
</Docs>
</MemberGroup>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="protected Grammar ();" />
<MemberSignature Language="ILAsm" Value=".method familyhidebysig specialname rtspecialname instance void .ctor() cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor" />
<MemberSignature Language="VB.NET" Value="Protected Sub New ()" />
<MemberSignature Language="C++ CLI" Value="protected:
 Grammar();" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters />
<Docs>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class.</summary>
<remarks>To be added.</remarks>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.IO.Stream stream);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.IO.Stream stream) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.IO.Stream)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (stream As Stream)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.IO.Stream -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar stream" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::IO::Stream ^ stream);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="stream" Type="System.IO.Stream" />
</Parameters>
<Docs>
<param name="stream">A stream that describes a speech recognition grammar in a supported format.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a <see cref="T:System.IO.Stream" />.</summary>
<remarks>
<format type="text/markdown"><
- Grammars that have been compiled to a binary file with a .cfg file extension
This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
An SRGS grammar can define a root rule. To create a <xref:System.Speech.Recognition.Grammar> object from a stream and specify a root rule, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> or <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
To create a <xref:System.Speech.Recognition.Grammar> object from a stream and specify a base URI to use to resolve relative rule references, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
## Examples
The following example creates a speech recognition grammar from a local SRGS file (cities.xml) using a file stream. The content of the cities.xml file appears following C# example.
```csharp
// Load a cities grammar from an I/O stream and
// return the new grammar.
private static Grammar CreateGrammarFromStream()
{
string fileName = @"c:\temp\cities.xml";
Grammar citiesGrammar =
new Grammar(new FileStream(fileName, FileMode.Open));
citiesGrammar.Name = "Stream Cities Grammar";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->
<rule id="Main">
<item>
I would like to fly from <ruleref uri="#Cities"/>
to <ruleref uri="#Cities"/>
</item>
</rule>
<rule id="Cities" scope="public">
<one-of>
<item>Seattle</item>
<item>Los Angeles</item>
<item>New York</item>
<item>Miami</item>
</one-of>
</rule>
</grammar>
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="stream" /> describes a grammar that does not contain a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="stream" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">The stream does not contain a valid description of a grammar, or describes a grammar that contains a rule reference that cannot be resolved.</exception>
<altmember cref="T:System.IO.Stream" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.Speech.Recognition.GrammarBuilder builder);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.GrammarBuilder builder) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.Speech.Recognition.GrammarBuilder)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (builder As GrammarBuilder)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.Speech.Recognition.GrammarBuilder -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar builder" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::Speech::Recognition::GrammarBuilder ^ builder);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Parameters>
<Parameter Name="builder" Type="System.Speech.Recognition.GrammarBuilder" />
</Parameters>
<Docs>
<param name="builder">An instance of <see cref="T:System.Speech.Recognition.GrammarBuilder" /> that contains the constraints for the speech recognition grammar.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a <see cref="T:System.Speech.Recognition.GrammarBuilder" /> object.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Examples
The following example creates a speech recognition grammar using <xref:System.Speech.Recognition.Choices> and <xref:System.Speech.Recognition.GrammarBuilder> objects. The <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor creates a <xref:System.Speech.Recognition.Grammar> object from the <xref:System.Speech.Recognition.GrammarBuilder> object.
```csharp
// Create a grammar using a GrammarBuilder and return the new grammar.
private static Grammar CreateGrammarBuilderGrammar()
{
GrammarBuilder builder = new GrammarBuilder();
Choices cityChoice = new Choices (new string[]
{"Seattle", "New York", "Miami", "Los Angeles"});
builder.Append("I would like to fly from");
builder.Append(cityChoice);
builder.Append("to");
builder.Append(cityChoice);
Grammar citiesGrammar = new Grammar(builder);
citiesGrammar.Name = "GrammarBuilder Cities Grammar";
return citiesGrammar;
}
```
]]></format>
</remarks>
<altmember cref="T:System.Speech.Recognition.GrammarBuilder" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.Speech.Recognition.SrgsGrammar.SrgsDocument)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (srgsDocument As SrgsDocument)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar srgsDocument" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="srgsDocument" Type="System.Speech.Recognition.SrgsGrammar.SrgsDocument" />
</Parameters>
<Docs>
<param name="srgsDocument">The constraints for the speech recognition grammar.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from an <see cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" /> object.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This constructor does not pass any parameters to the initialization handler, and the <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> should not contain an initialization handler that requires arguments.
A <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> can have a root rule. To create a <xref:System.Speech.Recognition.Grammar> object that specifies a root rule, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> or <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
To create a speech recognition <xref:System.Speech.Recognition.Grammar> from an <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> and specify a base URI to use to resolve relative rule references, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
## Examples
The following example creates a speech recognition grammar in an <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> instance, which is then used to construct a <xref:System.Speech.Recognition.Grammar> object.
```csharp
private static Grammar CreateSrgsDocumentGrammar()
{
// Create the SrgsDocument.
SrgsDocument document = new SrgsDocument();
// Create the Cities rule and add it to the document.
SrgsRule citiesRule = new SrgsRule("Cities");
SrgsOneOf cityChoice = new SrgsOneOf();
cityChoice.Add(new SrgsItem("Seattle"));
cityChoice.Add(new SrgsItem("Los Angeles"));
cityChoice.Add(new SrgsItem("New York"));
cityChoice.Add(new SrgsItem("Miami"));
citiesRule.Add(cityChoice);
document.Rules.Add(citiesRule);
// Create the Main rule and add it to the document.
SrgsRule mainRule = new SrgsRule("Main");
mainRule.Scope = SrgsRuleScope.Public;
SrgsItem item = new SrgsItem("I would like to fly from");
item.Add(new SrgsRuleRef(citiesRule));
item.Add(new SrgsText("to"));
item.Add(new SrgsRuleRef(citiesRule));
mainRule.Add(item);
document.Rules.Add(mainRule);
// Set the root rule.
document.Root = mainRule;
// Create the Grammar object.
Grammar citiesGrammar = new Grammar(document);
citiesGrammar.Name = "SrgsDocument Cities Grammar";
return citiesGrammar;
}
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="srgsDocument" /> does not contain a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="srgsDocument" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">
<paramref name="srgsDocument" /> contains a rule reference that cannot be resolved.</exception>
<altmember cref="N:System.Speech.Recognition.SrgsGrammar" />
<altmember cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (string path);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string path) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.String)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (path As String)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : string -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar path" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::String ^ path);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="path" Type="System.String" />
</Parameters>
<Docs>
<param name="path">The path of the file that describes a speech recognition grammar in a supported format.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a file.</summary>
<remarks>
<format type="text/markdown"><]
## Remarks
This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.
This constructor can create a <xref:System.Speech.Recognition.Grammar> instance from the following formats:
- XML-format files that conform to the W3C [Speech Recognition Grammar Specification (SRGS) Version 1.0](https://go.microsoft.com/fwlink/?LinkId=201761)
- Grammars that have been compiled to a binary file with a .cfg file extension
This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
An SRGS grammar can define a root rule. To create a <xref:System.Speech.Recognition.Grammar> object from a string and specify a root rule, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
To create a <xref:System.Speech.Recognition.Grammar> object that specifies a base URI to use to resolve relative rule references, open the file in a file stream and use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A?displayProperty=nameWithType> constructor.
## Examples
The following example loads a speech recognition grammar from a local SRGS file to build a <xref:System.Speech.Recognition.Grammar> object. The content of the cities.xml file appears in the XML example that follows the C# example.
```csharp
// Load a cities grammar from a local file and
// return the new grammar.
private static Grammar CreateGrammarFromFile()
{
Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml");
citiesGrammar.Name = "SRGS File Cities Grammar";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->
<rule id="Main">
<item>
I would like to fly from <ruleref uri="#Cities"/>
to <ruleref uri="#Cities"/>
</item>
</rule>
<rule id="Cities" scope="public">
<one-of>
<item>Seattle</item>
<item>Los Angeles</item>
<item>New York</item>
<item>Miami</item>
</one-of>
</rule>
</grammar>
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="path" /> contains the empty string (""), or the file describes a grammar that does not contain a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="path" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">The file does not contain a valid description, or describes a grammar that contains a rule reference that cannot be resolved.</exception>
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.IO.Stream stream, string ruleName);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.IO.Stream stream, string ruleName) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.IO.Stream,System.String)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (stream As Stream, ruleName As String)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.IO.Stream * string -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar (stream, ruleName)" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="stream" Type="System.IO.Stream" />
<Parameter Name="ruleName" Type="System.String" />
</Parameters>
<Docs>
<param name="stream">A stream that describes a speech recognition grammar in a supported format.</param>
<param name="ruleName">The identifier of the rule to use as the entry point of the speech recognition grammar, or <see langword="null" /> to use the default root rule of the grammar description.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a <see cref="T:System.IO.Stream" /> and specifies a root rule.</summary>
<remarks>
<format type="text/markdown"><]
## Remarks
This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.
This constructor can create a <xref:System.Speech.Recognition.Grammar> instance from the following formats:
- XML-format files that conform to the W3C [Speech Recognition Grammar Specification (SRGS) Version 1.0](https://go.microsoft.com/fwlink/?LinkId=201761)
- Grammars that have been compiled to a binary file with a .cfg file extension
This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
To create a <xref:System.Speech.Recognition.Grammar> from a stream and specify a base URI to use to resolve relative rule references, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
## Examples
The following example loads a local SRGS file (cities.xml) from a file stream and specifies a rule to use as the root of the grammar. The content of the cities.xml file appears in the XML example that follows the C# example.
```csharp
// Load a cities grammar from an I/O stream, use a specific
// rule as the root of the grammar, and return the new grammar.
private static Grammar CreateGrammarFromStream2()
{
FileInfo file = new FileInfo(@"c:\temp\cities.xml");
Grammar citiesGrammar = new Grammar(file.OpenRead(), "Main");
citiesGrammar.Name = "Stream Cities Grammar 2";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->
<rule id="Main">
<item>
I would like to fly from <ruleref uri="#Cities"/>
to <ruleref uri="#Cities"/>
</item>
</rule>
<rule id="Cities" scope="public">
<one-of>
<item>Seattle</item>
<item>Los Angeles</item>
<item>New York</item>
<item>Miami</item>
</one-of>
</rule>
</grammar>
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="ruleName" /> cannot be resolved or is not public, or <paramref name="ruleName" /> is <see langword="null" /> and the grammar description does not define a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="stream" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">The stream does not contain a valid description or describes a grammar that contains a rule reference that cannot be resolved.</exception>
<altmember cref="T:System.IO.Stream" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.Speech.Recognition.SrgsGrammar.SrgsDocument srgsDocument, string ruleName) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.Speech.Recognition.SrgsGrammar.SrgsDocument,System.String)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (srgsDocument As SrgsDocument, ruleName As String)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.Speech.Recognition.SrgsGrammar.SrgsDocument * string -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar (srgsDocument, ruleName)" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::Speech::Recognition::SrgsGrammar::SrgsDocument ^ srgsDocument, System::String ^ ruleName);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="srgsDocument" Type="System.Speech.Recognition.SrgsGrammar.SrgsDocument" />
<Parameter Name="ruleName" Type="System.String" />
</Parameters>
<Docs>
<param name="srgsDocument">The constraints for the speech recognition grammar.</param>
<param name="ruleName">The identifier of the rule to use as the entry point of the speech recognition grammar, or <see langword="null" /> to use the default root rule of the <see cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" />.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from an <see cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" /> object and specifies a root rule.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
This constructor does not pass any parameters to the initialization handler, and the <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> should not contain an initialization handler that requires arguments.
To create a <xref:System.Speech.Recognition.Grammar> object from a <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> and specify a base URI to use to resolve relative rule references, use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
## Examples
The following example creates a speech recognition grammar in an <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> instance and specifies a rule to use as the root rule of the grammar. The example constructs a <xref:System.Speech.Recognition.Grammar> object from the <xref:System.Speech.Recognition.SrgsGrammar.SrgsDocument> instance and loads it into the speech recognition engine.
```csharp
using System;
using System.Speech.Recognition;
using System.Speech.Recognition.SrgsGrammar;
namespace SampleRecognition
{
class Program
{
static void Main(string[] args)
// Initialize an in-process speech recognition engine.
{
using (SpeechRecognitionEngine recognizer =
new SpeechRecognitionEngine())
{
// Create the SrgsDocument.
SrgsDocument document = new SrgsDocument();
// Create the Cities rule and add it to the document.
SrgsRule citiesRule = new SrgsRule("Cities");
citiesRule.Scope = SrgsRuleScope.Public;
SrgsOneOf cityChoice = new SrgsOneOf();
cityChoice.Add(new SrgsItem("Seattle"));
cityChoice.Add(new SrgsItem("Los Angeles"));
cityChoice.Add(new SrgsItem("New York"));
cityChoice.Add(new SrgsItem("Miami"));
citiesRule.Add(cityChoice);
document.Rules.Add(citiesRule);
// Create the Main rule and add it to the document.
SrgsRule mainRule = new SrgsRule("Main");
mainRule.Scope = SrgsRuleScope.Public;
mainRule.Add(new SrgsItem("I would like to fly from"));
mainRule.Add(new SrgsRuleRef(citiesRule));
mainRule.Add(new SrgsItem("to"));
mainRule.Add(new SrgsRuleRef(citiesRule));
document.Rules.Add(mainRule);
// Create the Grammar object and specify which rule to use as the root.
Grammar citiesGrammar = new Grammar(document,"Main");
// Load the grammar object to the recognizer.
recognizer.LoadGrammarAsync(citiesGrammar);
// Attach a handler for the SpeechRecognized event.
recognizer.SpeechRecognized +=
new EventHandler<SpeechRecognizedEventArgs>(recognizer_SpeechRecognized);
// Set the input to the recognizer.
recognizer.SetInputToDefaultAudioDevice();
// Start recognition.
recognizer.RecognizeAsync();
Console.WriteLine("Starting asynchronous recognition...");
// Keep the console window open.
Console.ReadLine();
}
}
// Handle the SpeechRecognized event.
static void recognizer_SpeechRecognized(object sender, SpeechRecognizedEventArgs e)
{
Console.WriteLine(" Speech recognized: " + e.Result.Text);
}
}
}
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="ruleName" /> cannot be resolved or is not public, or <paramref name="ruleName" /> is <see langword="null" /> and <paramref name="srgsDocument" /> does not contain a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="srgsDocument" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">
<paramref name="srgsDocument" /> contains a rule reference that cannot be resolved.</exception>
<altmember cref="N:System.Speech.Recognition.SrgsGrammar" />
<altmember cref="T:System.Speech.Recognition.SrgsGrammar.SrgsDocument" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (string path, string ruleName);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(string path, string ruleName) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.String,System.String)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (path As String, ruleName As String)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : string * string -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar (path, ruleName)" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::String ^ path, System::String ^ ruleName);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="path" Type="System.String" />
<Parameter Name="ruleName" Type="System.String" />
</Parameters>
<Docs>
<param name="path">The path of the file that describes a speech recognition grammar in a supported format.</param>
<param name="ruleName">The identifier of the rule to use as the entry point of the speech recognition grammar, or <see langword="null" /> to use the default root rule of the grammar description.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a file and specifies a root rule.</summary>
<remarks>
<format type="text/markdown"><]
## Remarks
This constructor does not pass any parameters to the initialization handler, and the description should not define an initialization handler that requires arguments.
This constructor can create a <xref:System.Speech.Recognition.Grammar> instance from the following formats:
- XML-format files that conform to the W3C [Speech Recognition Grammar Specification (SRGS) Version 1.0](https://go.microsoft.com/fwlink/?LinkId=201761)
- Grammars that have been compiled to a binary file with a .cfg file extension
This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
To create a <xref:System.Speech.Recognition.Grammar> that specifies a base URI to use to resolve relative rule references, open a file stream for the file and use the <xref:System.Speech.Recognition.Grammar.%23ctor%2A> constructor.
## Examples
The following example loads a local SRGS file (cities.xml) from a file and specifies a rule to use as the root of the grammar. The content of the cities.xml file appears in the XML example that follows the C# example.
```csharp
// Load a cities grammar from a local file, use a specific
// rule as the root of the grammar, and return the new grammar.
private static Grammar CreateGrammarFromFile2()
{
Grammar citiesGrammar = new Grammar(@"c:\temp\cities.xml", "Main");
citiesGrammar.Name = "SRGS File Cities Grammar 2";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->
<rule id="Main">
<item>
I would like to fly from <ruleref uri="#Cities"/>
to <ruleref uri="#Cities"/>
</item>
</rule>
<rule id="Cities" scope="public">
<one-of>
<item>Seattle</item>
<item>Los Angeles</item>
<item>New York</item>
<item>Miami</item>
</one-of>
</rule>
</grammar>
```
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="ruleName" /> cannot be resolved or is not public, <paramref name="path" /> is the empty string (""), or <paramref name="ruleName" /> is <see langword="null" /> and the grammar description does not define a root rule.</exception>
<exception cref="T:System.ArgumentNullException">
<paramref name="path" /> is <see langword="null" />.</exception>
<exception cref="T:System.FormatException">The file does not contain a valid description or describes a grammar that contains a rule reference that cannot be resolved.</exception>
<altmember cref="T:System.Speech.Recognition.SpeechRecognitionEngine" />
<altmember cref="T:System.Speech.Recognition.SpeechRecognizer" />
<related type="ExternalDocumentation" href="https://www.w3.org/TR/speech-grammar/">Speech Recognition Grammar Specification</related>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.IO.Stream stream, string ruleName, object[] parameters);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.IO.Stream stream, string ruleName, object[] parameters) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.IO.Stream,System.String,System.Object[])" />
<MemberSignature Language="VB.NET" Value="Public Sub New (stream As Stream, ruleName As String, parameters As Object())" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.IO.Stream * string * obj[] -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar (stream, ruleName, parameters)" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName, cli::array <System::Object ^> ^ parameters);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="stream" Type="System.IO.Stream" />
<Parameter Name="ruleName" Type="System.String" />
<Parameter Name="parameters" Type="System.Object[]" />
</Parameters>
<Docs>
<param name="stream">A <see cref="T:System.IO.Stream" /> connected to an input/output object (including files, VisualStudio Resources, and DLLs) that contains a grammar specification.</param>
<param name="ruleName">The identifier of the rule to use as the entry point of the speech recognition grammar, or <see langword="null" /> to use the default root rule of the grammar description.</param>
<param name="parameters">Parameters to be passed to the initialization handler specified by the <see cref="P:System.Speech.Recognition.SrgsGrammar.SrgsRule.OnInit" /> property for the entry point or the root rule of the <see cref="T:System.Speech.Recognition.Grammar" /> to be created. This parameter may be null.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a <see cref="T:System.IO.Stream" /> and specifies a root rule.</summary>
<remarks>
<format type="text/markdown"><![CDATA[
## Remarks
Parameters for an initialization handler may also be specified.
]]></format>
</remarks>
<exception cref="T:System.ArgumentException">
<paramref name="stream" /> is connected to a grammar that:
- Does not contain the rule specified in <paramref name="ruleName" />
- Requires initialization parameters different from those specified in <paramref name="parameters" />
- Contains a relative rule reference that cannot be resolved by the default base <see cref="T:System.Uri" /> rule for grammars.</exception>
</Docs>
</Member>
<Member MemberName=".ctor">
<MemberSignature Language="C#" Value="public Grammar (System.IO.Stream stream, string ruleName, Uri baseUri);" />
<MemberSignature Language="ILAsm" Value=".method public hidebysig specialname rtspecialname instance void .ctor(class System.IO.Stream stream, string ruleName, class System.Uri baseUri) cil managed" />
<MemberSignature Language="DocId" Value="M:System.Speech.Recognition.Grammar.#ctor(System.IO.Stream,System.String,System.Uri)" />
<MemberSignature Language="VB.NET" Value="Public Sub New (stream As Stream, ruleName As String, baseUri As Uri)" />
<MemberSignature Language="F#" Value="new System.Speech.Recognition.Grammar : System.IO.Stream * string * Uri -> System.Speech.Recognition.Grammar" Usage="new System.Speech.Recognition.Grammar (stream, ruleName, baseUri)" />
<MemberSignature Language="C++ CLI" Value="public:
 Grammar(System::IO::Stream ^ stream, System::String ^ ruleName, Uri ^ baseUri);" />
<MemberType>Constructor</MemberType>
<AssemblyInfo>
<AssemblyName>System.Speech</AssemblyName>
<AssemblyVersion>3.0.0.0</AssemblyVersion>
<AssemblyVersion>4.0.0.0</AssemblyVersion>
<AssemblyVersion>10.0.0.0</AssemblyVersion>
</AssemblyInfo>
<Attributes>
<Attribute FrameworkAlternate="net-10.0-pp;net-8.0-pp;net-9.0-pp;netframework-4.7.1-pp;netframework-4.7.2-pp;netframework-4.8.1-pp;netframework-4.8-pp;netstandard-2.0-pp">
<AttributeName Language="C#">[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)]</AttributeName>
<AttributeName Language="F#">[<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-3.0;netframework-3.5;netframework-4.0;netframework-4.5;netframework-4.5.1;netframework-4.5.2;netframework-4.6;netframework-4.6.1;netframework-4.6.2;netframework-4.7;netframework-4.7.1;netframework-4.7.2;netframework-4.8;netframework-4.8.1">
<AttributeName Language="C#">[System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)]</AttributeName>
<AttributeName Language="F#">[<System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Advanced)>]</AttributeName>
</Attribute>
<Attribute FrameworkAlternate="netframework-4.0">
<AttributeName Language="C#">[System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]</AttributeName>
<AttributeName Language="F#">[<System.Runtime.TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")>]</AttributeName>
</Attribute>
</Attributes>
<Parameters>
<Parameter Name="stream" Type="System.IO.Stream" />
<Parameter Name="ruleName" Type="System.String" />
<Parameter Name="baseUri" Type="System.Uri" />
</Parameters>
<Docs>
<param name="stream">A stream that describes a speech recognition grammar in a supported format.</param>
<param name="ruleName">The identifier of the rule to use as the entry point of the speech recognition grammar, or <see langword="null" /> to use the default root rule of the grammar description.</param>
<param name="baseUri">The base URI to use to resolve any relative rule reference in the grammar description, or <see langword="null" />.</param>
<summary>Initializes a new instance of the <see cref="T:System.Speech.Recognition.Grammar" /> class from a stream, specifies a root rule, and defines a base Uniform Resource Identifier (URI) to resolve relative rule references.</summary>
<remarks>
<format type="text/markdown"><
- Grammars that have been compiled to a binary file with a .cfg file extension
This constructor compiles XML-format grammar files to a binary format to optimize them for loading and consumption by a speech recognition engine. You can reduce the amount of time required to construct a <xref:System.Speech.Recognition.Grammar> object from an XML-format grammar by compiling the grammar in advance, using one of the <xref:System.Speech.Recognition.SrgsGrammar.SrgsGrammarCompiler.Compile%2A> methods.
This constructor does not validate `baseUri`. However, the `LoadGrammar` method of a <xref:System.Speech.Recognition.SpeechRecognitionEngine> or <xref:System.Speech.Recognition.SpeechRecognizer> object throws an exception if it cannot resolve all of the rule references in the grammar description. If `baseUri` is not `null`, the `LoadGrammar` method uses the URI to resolve any rule references that it cannot otherwise resolve. If `baseUri` represents a file, then the `LoadGrammar` uses both the designated file and the file's directory when it attempts to resolve relative rule references.
[!INCLUDE [untrusted-data-instance-note](~/includes/untrusted-data-instance-note.md)]
## Examples
The following example loads a local SRGS file (shuttle.xml) from a file stream. The file contains a relative rule reference to a rule in the cities.xml file, and specifies a base URI to use to resolve the rule reference. The content of the shuttle.xml and cities.xml files appears in the XML examples that follow the C# example.
```csharp
private static Grammar CreateGrammarFromStream3()
{
FileInfo file = new FileInfo(@".\shuttle.xml");
Uri baseUri = new Uri(@"file://c:\temp\");
Grammar citiesGrammar = new Grammar(file.OpenRead(), null, baseUri);
citiesGrammar.Name = "Stream Cities Grammar 3";
return citiesGrammar;
}
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- shuttle.xml:
Defines an SRGS grammar for asking about a shuttle service. This grammar
references a Cities rule that is defined in the cities.xml grammar. -->
<rule id="Main">
<item>
Can I get a shuttle in
<ruleref uri="cities.xml#Cities"/>
</item>
</rule>
</grammar>
```
```xml
<?xml version="1.0" encoding="UTF-8" ?>
<grammar version="1.0" xml:lang="en-US"
xmlns="http://www.w3.org/2001/06/grammar"
tag-format="semantics/1.0" root="Main">
<!-- cities.xml:
Defines an SRGS grammar for requesting a flight. This grammar includes
a Cities rule that lists the cities that can be used for departures
and destinations. -->