Code Snippets Schema Reference

 

For the latest documentation on Visual Studio 2017 RC, see Visual Studio 2017 RC Documentation.

IntelliSense Code Snippets are pre-authored pieces of code that are ready to be inserted into your application with Visual Studio. You can increase productivity by providing code snippets that reduce the amount of time spent typing repetitive code or searching for samples. You can use the IntelliSense Code Snippet XML schema to create your own code snippets and add them to the code snippets that Visual Studio already includes.

Assembly ElementHelpUrl ElementReferences Element
Author ElementID ElementShortcut Element
Code ElementImport ElementSnippet Element
CodeSnippet ElementImports ElementSnippetType Element
CodeSnippets ElementKeyword ElementSnippetTypes Element
Declarations ElementKeywords ElementTitle Element
Default ElementLiteral ElementToolTip Element
Description ElementNamespace ElementType Element
Function ElementObject ElementUrl Element
Header ElementReference Element

Specifies the name of the assembly referenced by the code snippet.

System_CAPS_ICON_note.jpg Note

The Assembly element is only supported by Visual Basic code snippets.

The text value of the Assembly element is either the friendly text name of the assembly, such as System.dll, or its strong name, such as System,Version=1.0.0.1,Culture=neutral,PublicKeyToken=9b35aa323c18d4fb1.

<Assembly>  
    AssemblyName  
</Assembly>  

Parent ElementDescription
Reference ElementContains information about assembly references required by the code snippet.

A text value is required. This text specifies the assembly that the code snippet references.

Specifies the name of the snippet author. The Code Snippets Manager displays the name stored in the Author element of the code snippet.

<Author>  
   Code Snippet Author  
</Author>  
  

Parent ElementDescription
Header ElementContains general information about the code snippet.

A text value is required. This text specifies the author of the code snippet.

Provides a container for short code blocks.

Two reserved words are available for use in the text of the Code element: $end$ and $selected$. $end$ marks the location to place the cursor after the code snippet is inserted. $selected$ represents text selected in the document that is to be inserted into the snippet when it is invoked. For example, given a snippet that includes:

$selected$ is a great color.  

If the word "Blue" is selected when the user invokes the template, the result is:

Blue is a great color.  

You may not use either $end$ or $selected$ more than once in a code snippet. If you do, only the second instance is recognized. Given a snippet that includes:

$selected$ is a great color. I love $selected$.  

If the word "Blue" is selected, the result is:

is a great color. I love Blue.  

The initial space appears because there is a space between $selected$ and is.

All other $ keywords are dynamically defined in the <Literal> and <Object> tags.

<Code Language="Language"  
    Kind="method body/method decl/type decl/page/file/any"  
    Delimiter="Delimiter">  
    Code to insert  
</Code>  

AttributeDescription
DelimiterOptional attribute. Specifies the delimiter used to describe literals and objects in the code. By default, the delimiter is $.
KindOptional attribute. Specifies the kind of code that the snippet contains and the location at which a code snippet must be inserted for the code snippet to compile. The values available are method body, method decl, type decl, file, and any.
LanguageRequired attribute. Specifies the language of the code snippet.
Kind Attribute ValueDescription
method bodySpecifies that the code snippet is a method body, and therefore, must be inserted inside a method declaration.
method declSpecifies that the code snippet is a method, and therefore, must be inserted inside a class or module.
type declSpecifies that the code snippet is a type, and therefore, must be inserted inside a class, module, or namespace.
fileSpecifies that the snippet is a full code file. These code snippets can be inserted alone into a code file, or inside a namespace.
anySpecifies that the snippet can be inserted anywhere. This tag is used for code snippets that are context-independent, such as comments.
Language Attribute ValueDescription
VBIdentifies a Visual Basic code snippet.
CSharpIdentifies a C# code snippet.
CPPIdentifies a C++ code snippet.
XMLIdentifies an XML code snippet.
JavaScriptIdentifies a JavaScript code snippet.
SQLIdentifies a SQL code snippet.
HTMLIdentifies an HTML code snippet.
Parent ElementDescription
Snippet ElementContains the references, imports, declarations, and code for the code snippet.

A text value is required. This text specifies the code, along with the literals and objects, that you can use when this code snippet is inserted into a project.

Allows you to specify a heading and multiple IntelliSense Code Snippets, which you can insert into Visual Studio code files.

<CodeSnippet Format="x.x.x">  
    <Header>... </Header>  
    <Snippet>... </Snippet>  
</CodeSnippet>  
  

AttributeDescription
FormatRequired attribute. Specifies the schema version of the code snippet. The Format attribute must be a string in the syntax of x.x.x, where each "x" represents a numerical value of the version number. Visual Studio will ignore code snippets with Format attributes that it does not understand.
Child ElementDescription
Header ElementRequired element. Contains general information about the code snippet. There must be exactly one Header element in a code snippet.
Snippet ElementRequired element. Contains the code that will be inserted by Visual Studio. There must be exactly one Snippet element in a code snippet.
Parent ElementDescription
CodeSnippets ElementRoot element of the code snippet XML schema.

Groups CodeSnippet Elementelements. The CodeSnippets element is the root element of the code snippet XML schema.

<CodeSnippets>  
    <CodeSnippet>... </CodeSnippet>  
</CodeSnippets>  
  

Child ElementDescription
CodeSnippet ElementOptional element. Parent element for all code snippet data. There may be zero or more CodeSnippet elements in a CodeSnippets element.

Specifies the literals and objects that make up the parts of a code snippet that you can edit.

<Declarations>  
    <Literal>... </Literal>  
    <Object>... </Object>  
</Declarations>  
  

Child ElementDescription
Literal ElementOptional element. Defines the literals of the code snippet that you can edit. There may be zero or more Literal elements in a Declarations element.
Object ElementOptional element. Defines the objects of the code snippet that you can edit. There may be zero or more Object elements in a Declarations element.
Parent ElementDescription
Snippet ElementContains the references, imports, declarations, and code for the code snippet.

Specifies the default value of the literal or object for an IntelliSense Code Snippet.

<Default>  
    Default value  
</Default>  
  

Parent ElementDescription
Literal ElementDefines the literal fields of the code snippet that you can edit.
Object ElementDefines the object fields of the code snippet that you can edit.

A text value is required. This text specifies the default value of the literal or object that fills the fields of the code snippet that you can edit.

Specifies descriptive information about the contents of an IntelliSense Code Snippet.

<Description>  
    Code Snippet Description  
</Description>  

Parent ElementDescription
Header ElementContains general information about the code snippet.

A text value is required. This text describes the code snippet.

Specifies a function to execute when the literal or object receives focus in Visual Studio.

System_CAPS_ICON_note.jpg Note

The Function element is only supported in Visual C# code snippets.

<Function>  
    FunctionName  
</Function>  

Parent ElementDescription
Literal ElementDefines the literal fields of the code snippet that you can edit.
Object ElementDefines the object fields of the code snippet that you can edit.

A text value is required. This text specifies a function to execute when the literal or object field receives focus in Visual Studio.

Specifies general information about the IntelliSense Code Snippet.

<Header>  
    <Title>... </Title>  
    <Author>... </Author>  
    <Description>... </Description>  
    <HelpUrl>... </HelpUrl>  
    <SnippetTypes>... </SnippetTypes>  
    <Keywords>... </Keywords>  
    <Shortcut>... </Shortcut>  
</Header>  
  

Child ElementDescription
Author ElementOptional element. The name of the person or company that authored the code snippet. There may be zero or one Author elements in a Header element.
Description ElementOptional element. A description of the code snippet. There may be zero or one Description elements in a Header element.
HelpUrl ElementOptional element. A URL that contains more information about the code snippet. There may be zero or one HelpURL elements in a Header element. Note: Visual Studio does not use the HelpUrl element. The element is part of the IntelliSense Code Snippet XML schema and any code snippet containing the element will validate, but the value of the element is never used.
Keywords ElementOptional element. Groups Keyword elements. There may be zero or one Keywords elements in a Header element.
Shortcut ElementOptional element. Specifies the shortcut text that can be used to insert the snippet. There may be zero or one Shortcut elements in a Header element.
SnippetTypes ElementOptional element. Groups SnippetType elements. There may be zero or one SnippetTypes elements in a Header element. If there are no SnippetTypes elements, the code snippet is always valid.
Title ElementRequired element. The friendly name of the code snippet. There must be exactly one Title element in a Header element.
Parent ElementDescription
CodeSnippet ElementParent element for all code snippet data.

Specifies a URL that provides more information about a code snippet.

System_CAPS_ICON_note.jpg Note

Visual Studio does not use the HelpUrl element. The element is part of the IntelliSense Code Snippet XML schema and any code snippet containing the element will validate, but the value of the element is never used.

<HelpUrl>  
    www.microsoft.com  
</HelpUrl>  
  

Parent ElementDescription
Header ElementContains general information about the code snippet.

A text value is optional. This text specifies the URL to visit for more information about a code snippet.

Specifies a unique identifier for a Literal or Object element. No two literals or objects in the same code snippet can have the same text value in their ID elements.Literals and objects cannot contain an ID element with a value of end. The value $end$ is reserved, and is used to mark the location to place the cursor after the code snippet is inserted.

<ID>  
    Unique Identifier  
</ID>  
  

Parent ElementDescription
Literal ElementDefines the literal fields of the code snippet that you can edit.
Object ElementDefines the object fields of the code snippet that you can edit.

A text value is required. This text specifies the unique identifier for the object or literal.

Specifies the imported namespaces used by an IntelliSense Code Snippet.

System_CAPS_ICON_note.jpg Note

The Import element is only supported for Visual Basic projects.

<Import>  
    <Namespace>... </Namespace>  
</Import>  
  

Child ElementDescription
Namespace ElementRequired element. Specifies the namespace used by the code snippet. There must be exactly one Namespace element in an Import element.
Parent ElementDescription
Imports ElementGrouping element for Import elements.

Groups individual Import elements.

System_CAPS_ICON_note.jpg Note

The Imports element is only supported for Visual Basic projects.

<Imports>  
    <Import>... </Import>  
<Imports>  

Child ElementDescription
Import ElementOptional element. Contains the imported namespaces for the code snippet. There may be zero or more Import elements in an Imports element.
Parent ElementDescription
Snippet ElementContains the references, imports, declarations, and code for the code snippet.

Specifies a custom keyword for the code snippet. The code snippet keywords are used by Visual Studio and represent a standard way for online content providers to add custom keywords for searching or categorization.

<Keyword>  
    Code Snippet Keyword  
</Keyword>  

Parent ElementDescription
Keywords ElementGroups individual Keyword elements.

A text value is required. The keyword for the code snippet.

Groups individual Keyword elements. The code snippet keywords are used by Visual Studio and represent a standard way for online content providers to add custom keywords for searching or categorization

<Keywords>  
    <Keyword>... </Keyword>  
    <Keyword>... </Keyword>  
<Keywords>  

Child ElementDescription
Keyword ElementOptional element. Contains individual keywords for the code snippet. There may be zero or more Keyword elements in a Keywords element.
Parent ElementDescription
Header ElementContains general information about the code snippet.

Defines the literals of the code snippet that you can edit. The Literal element is used to identify a replacement for a piece of code that is entirely contained within the snippet, but will likely be customized after it is inserted into the code. For example, literal strings, numeric values, and some variable names should be declared as literals.

Literals and objects cannot contain an ID element with a value of selected or end. The value $selected$ represents text selected in the document that is to be inserted into the snippet when it is invoked. $end$ marks the location to place the cursor after the code snippet is inserted.

<Literal Editable="true/false">  
   <ID>... </ID>  
   <ToolTip>... </ToolTip>  
   <Default>... </Default>  
   <Function>... </Function>  
</Literal>  

AttributeDescription
EditableOptional Boolean attribute. Specifies whether or not you can edit the literal after the code snippet is inserted. The default value of this attribute is true.
Child ElementDescription
Default ElementRequired element. Specifies the literal's default value when you insert the code snippet. There must be exactly one Default element in a Literal element.
Function ElementOptional element. Specifies a function to execute when the literal receives focus in Visual Studio. There may be zero or one Function elements in a Literal element.
ID ElementRequired element. Specifies a unique identifier for the literal. There must be exactly one ID element in a Literal element.
ToolTip ElementOptional element. Describes the expected value and usage of the literal. There may be zero or one Tooltip elements in a Literal element.
Parent ElementDescription
Declarations ElementContains the literals and objects of a code snippet that you can edit.

Specifies the namespace that must be imported for the code snippet to compile and run. The namespace specified in the Namespace element is automatically added to an Imports statement at the beginning of the code, if it does not already exist.

System_CAPS_ICON_note.jpg Note

The Namespace element is only supported for Visual Basic projects.

<Namespace>  
    Namespace  
</Namespace>  

Parent ElementDescription
Import ElementImports the specified namespace.

A text value is required. This text specifies a namespace that the code snippet assumes is imported.

Defines the objects of the code snippet that you can edit. The Object element is used to identify an item that is required by the code snippet but is likely to be defined outside of the snippet itself. For example, Windows Forms controls, ASP.NET controls, object instances, and type instances should be declared as objects. Object declarations require that a type be specified, which is done with the Type element.

<Object Editable="true/false">  
    <ID>... </ID>  
    <Type>... </Type>  
    <ToolTip>... </ToolTip>  
    <Default>... </Default>  
    <Function>... </Function>  
</Object>  

AttributeDescription
EditableOptional Boolean attribute. Specifies whether or not you can edit the literal after the code snippet is inserted. The default value of this attribute is true.
Child ElementDescription
Default ElementRequired element. Specifies the literal's default value when you insert the code snippet. There must be exactly one Default element in a Literal element.
Function ElementOptional element. Specifies a function to execute when the literal receives focus in Visual Studio. There may be zero or one Function elements in a Literal element.
ID ElementRequired element. Specifies a unique identifier for the literal. There must be exactly one ID element in a Literal element.
ToolTip ElementOptional element. Describes the expected value and usage of the literal. There may be zero or one Tooltip elements in a Literal element.
Type ElementRequired element. Specifies the type of the object. There must be exactly one Type element in an Object element.
Parent ElementDescription
Declarations ElementContains the literals and objects of a code snippet that you can edit.

Specifies information about the assembly references required by the code snippet.

System_CAPS_ICON_note.jpg Note

The Reference element is only supported for Visual Basic projects.

<Reference>  
    <Assembly>... </Assembly>  
    <Url>... </Url>  
</Reference>  

Child ElementDescription
Assembly ElementRequired element. Contains the name of the assembly referenced by the code snippet. There must be exactly one Assembly element in a Reference element.
Url ElementOptional element. Contains a URL that provides more information about the referenced assembly. There may be zero or one Url elements in a Reference element.
Parent ElementDescription
References ElementGrouping element for Reference elements.

Groups individual Reference elements.

System_CAPS_ICON_note.jpg Note

The References element is only supported for Visual Basic projects.

<References>  
    <Reference>... </Reference>  
</References>  

Child ElementDescription
Reference ElementOptional element. Contains information about assembly references for the code snippet. There may be zero or more Reference elements in a References element.
Parent ElementDescription
Snippet ElementContains the references, imports, declarations, and code for the code snippet.

Specifies the shortcut text used to insert the snippet. The text value of a Shortcut element can only contain alphanumeric characters, hyphens ( - ), and underscores ( _ ).

System_CAPS_ICON_caution.jpg Caution

_ and – are not supported characters in C++ snippet shortcuts.

<Shortcut>  
    Shortcut Text  
</Shortcut>  

Parent ElementDescription
Header ElementContains general information about the code snippet.

A text value is optional. This text is used as a shortcut for inserting the code snippet.

Specifies the references, imports, declarations, and code for the code snippet.

<Snippet>  
    <References>... </References>  
    <Imports>... </Imports>  
    <Declarations>... </Declarations>  
    <Code>... </Code>  
</Snippet>  
  

Child ElementDescription
Code ElementRequired element. Specifies the code that you want to insert into a documentation file. There must be exactly one Code element in a Snippet element.
Declarations ElementOptional element. Specifies the literals and objects that make up the parts of a code snippet that you can edit. There may be zero or one Declarations elements in a Snippet element.
Imports ElementOptional element. Groups individual Import elements. There may be zero or one Imports elements in a Snippet element.
Optional element. Groups individual Reference elements. There may be zero or one References elements in a Snippet element.
Parent ElementDescription
CodeSnippet ElementAllows you to specify a heading and multiple IntelliSense Code Snippets, which you can insert into Visual Studio code files.

Specifies how Visual Studio inserts the code snippet.

<SnippetType>  
    SurroundsWith/Expansion  
<SnippetType>  

Parent ElementDescription
SnippetTypes ElementGroups SnippetType elements.

The text value must be one of the following values:

  • SurroundsWith: allows the code snippet to be placed around a selected piece of code.

  • Expansion: allows the code snippet to be inserted at the cursor.

  • Refactoring: specifies that the code snippet is used during Visual C# refactoring. Refactoring cannot be used in custom code snippets.

Groups individual SnippetType elements. If the SnippetTypes element is not present, the code snippet can be inserted anywhere in the code.

<SnippetTypes>  
    <SnippetType>... </SnippetType>  
    <SnippetType>... </SnippetType>  
<SnippetTypes>  

Child ElementDescription
SnippetType ElementOptional element. Specifies how Visual Studio inserts the code snippet into the code. There may be zero or more SnippetType elements in a SnippetTypes element.
Parent ElementDescription
Header ElementSpecifies general information about the code snippet.

Specifies the title for the code snippet. The title stored in the Title element of the code snippet appears in the Code Snippet Picker and in the code snippet's description in the Code Snippets Manager.

<Title>  
    Code Snippet Title  
<Title>  

Parent ElementDescription
Header ElementSpecifies general information about the code snippet.

A text value is required. This text specifies the title of the code snippet.

Describes the expected value and usage of a literal or object in a code snippet, which Visual Studio displays in a ToolTip when it inserts the code snippet into a project. The ToolTip text is displayed when the mouse hovers over the literal or object after the code snippet has been inserted.

<ToolTip>  
    ToolTip description  
</ToolTip>  

Parent ElementDescription
Literal ElementDefines the literal fields of the code snippet that you can edit.
Object ElementDefines the object fields of the code snippet that you can edit.

A text value is required. This text specifies the ToolTip description to be associated with the object or literal in the code snippet.

Specifies the type of the object. The Object element is used to identify an item that is required by the code snippet but is likely to be defined outside of the snippet itself. For example, Windows Forms controls, ASP.NET controls, object instances, and type instances should be declared as objects. Object declarations require that a type be specified, which is done with the Type element.

<Type>  
    Type  
</Type>  

Parent ElementDescription
Object ElementDefines the object fields of the code snippet that you can edit.

A text value is required. This text specifies the type of the object.

Specifies a URL that provides more information about the referenced assembly.

System_CAPS_ICON_note.jpg Note

The Url element is only supported for Visual Basic projects.

<Url>  
    www.microsoft.com  
</Url>  

Parent ElementDescription
Reference ElementSpecifies the assembly references required by the code snippet.

A text value is required. This text specifies a URL with more information about the referenced assembly. This URL is displayed when the reference cannot be added to the project.

Code Snippets
Walkthrough: Creating a Code Snippet

Show: