How to: Create a Basic Code Snippet
IntelliSense Code Snippets are XML files with a .snippet file name extension that adhere to the IntelliSense Code Snippet XML schema. In this topic, you will create a basic code snippet that displays a message box. For more information on the Code Snippet XML schema, see Code Snippets Schema Reference.
Creating an .snippet File
Before you can begin writing your code snippet, you must create an XML file with a .snippet file name extension.
To create a .snippet file
-
On the File menu, click New and then click File.
-
Click XML File and then click Open.
-
On the File menu, click Save <XMLFileName>.
-
In the Save as type box, select All Files (*.*).
-
In the File name box, enter a file name with the .snippet file name extension.
-
Click Save.
Writing the Code
Now that you have an XML file, you need to write the XML code that makes up your code snippet. For more information on any of the XML elements used in the following examples, see Code Snippets Schema Reference.
To write the code for the code snippet
-
Below the automatically generated line of XML, add a CodeSnippets element with the proper xmlns attribute value, and a CodeSnippet element to create an individual code snippet. For example:
<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> -
Add a header section to the code snippet. For example:
<Header> <Title> My Snippet </Title> </Header> -
Add the elements that define the code snippet itself. In the following example, the language of the code snippet is Visual Basic.
<Snippet> <Code Language="VB"> </Code> </Snippet>
Note The Language attribute also accepts the values CSharp, VJSharp, and XML. For more information, see Code Element (IntelliSense Code Snippets).
-
Inside the Code element, add the Visual Basic code for the snippet. All snippet code must be placed between <![CDATA[ and ]]> brackets. In the following example, the code that will be inserted is MessageBox.Show("Hello World").
<Code Language="VB"> <![CDATA[MessageBox.Show("Hello World")]]> </Code> -
Inside the Snippet element, add the References element and all of the required child elements that add a reference to the project when the snippet is inserted. In this example, the code snippet adds a reference to System.Windows.Forms.dll when the snippet is inserted.
Note Only Visual Basic code snippets support the References section. Code snippets in other languages require that the correct references be added manually.
<Snippet> <References> <Reference> <Assembly>System.Windows.Forms.dll</Assembly> </Reference> </References> <Code Language="VB"> <![CDATA[MessageBox.Show("Hello World")]]> </Code> </Snippet>
You can now use this IntelliSense Code Snippet in Visual Studio 2005 by following the procedures in How to: Manage Code Snippets and How to: Insert IntelliSense Code Snippets.
Example
This example contains the entire IntelliSense Code Snippet created in the previous steps.
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
My Snippet
</Title>
</Header>
<Snippet>
<References>
<Reference>
<Assembly>System.Windows.Forms.dll</Assembly>
</Reference>
</References>
<Code Language="VB">
<![CDATA[MessageBox.Show("Hello World")]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
This example contains a Visual C# version of the IntelliSense Code Snippet created in the previous steps. Visual C# code snippets to do not support the References section, so a reference to System.Windows.Forms.dll must be added to the project manually.
<CodeSnippets
xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<CodeSnippet Format="1.0.0">
<Header>
<Title>
My Snippet
</Title>
</Header>
<Snippet>
<Code Language="CSharp">
<![CDATA[MessageBox.Show("Hello World");]]>
</Code>
</Snippet>
</CodeSnippet>
</CodeSnippets>
See Also
1. ctrl+K+X (Insert snipet)
2. click on snipet.
this will create the following xml structure for snipet:
<CodeSnippet Format="1.0.0" xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<Header>
<Title>title</Title>
<Author>author</Author>
<Shortcut>shortcut</Shortcut>
<Description>description</Description>
<SnippetTypes>
<SnippetType>SurroundsWith</SnippetType>
<SnippetType>Expansion</SnippetType>
</SnippetTypes>
</Header>
<Snippet>
<Declarations>
<Literal>
<ID>name</ID>
<Default>value</Default>
</Literal>
</Declarations>
<Code Language="XML">
<![CDATA[<test>
<name>$name$</name>
$selected$ $end$</test>]]>
</Code>
</Snippet>
</CodeSnippet>
Now change code under CDATA and save as snipet file.
To use this file for your snipet's list, Import using code snipet manager(Tool>code snipet manager).
Enjoy GreenCoding @ code4green.com
- 1/20/2011
- Hussain Naqvi
- 4/19/2010
- Matt Manela
Thanks to the developer for sharing this tool with us.
Regards,
Pedro Martins
Portugal
- 4/10/2010
- Rechousa
hey guys... I have create my own snippet and there is:
<
CodeSnippetsxmlns="http://schemas.microsoft.com/VisualStudio/2008/CodeSnippet"><
CodeSnippetFormat="1.0.0"><
Header><
Title>propt</Title><
Shortcut>propt</Shortcut><
Description>Code Snippet para Declarao de propriedade em classe</Description><
Author>A.Fernando de Oliveira - aristotelesfernando.jr@gmail.com</Author></
Header><
Snippet><
CodeLanguage="CSharp"><![CDATA[
private int _myPropertie;public int MyPropoertie
{
get { return _myPropertie; }
set { _myPropertie = value; }
}
]]></
Code></
Snippet></
CodeSnippet></
CodeSnippets>
this create a code snippet for declare a propertie with the public and private
if someone need a help to create you own
- 2/16/2010
- Fernando JR
- 2/16/2010
- Fernando JR
If this annoys you so much, why don't you just build your own snippet generator? I am a rank amateur programmer and I think I could create a snippet generator in a weekend. A windows form, several text boxes and an XmlSerializer is all you'd need. Throw it up on a website with a donation button and you could make a little money for your trouble. Sure, Visual Studio should do this better, but it can't do everything for you.
- 12/5/2009
- cormachsmith
- 12/5/2009
- cormachsmith
- 10/4/2009
- DaddyUnit
http://www.codeplex.com/snippy
Check it out!
- 9/2/2009
- jhhhj
I agree with KansasCoder - snippets are great! Just "copying and pasting" code into a project is not a very sensible thing to do, by using xml to create snippets you can quickly insert code that can be easily customized and modified to suit the context that it is in. You do not need to learn much xml to get going and as KansasCoder pointed out you can always use a template.
Give it a try a quit all your moaning!
- 10/6/2008
- RagingBen
If typing a few xml elements is taxing you can save a basic template and then cut and paste your code snippets into the cdata section. I don't see a problem with the way the snippet manager was implemented except they could have provided more snippets for us.
Now please everyone get back to work. Your jobs are going to India!! Lol
- 9/19/2008
- KansasCoder
When in code view, the toolbox is still available but it is empty by default. You can drag code segments onto the toolbox and in effect create a snippet. It's just not read by intellisense. You can then double click that text to fill it's contents at the cursor position or drag it to the location where you want the snippets contents to appear. Unfortunately, your toolbox is wiped clean for every new project.
The snippets in this article are better geared towards code which require references and literals. I agree, it's a lot more work then it's worth. Even getting your snippets to be read by the intellisense or forcing intellisense to display snippets is unorthodox, awkward, and slightly 'out of the way'.
.NET treats XML like the second coming, and although useful at times, I think it's entirely over-rated.
- 8/23/2008
- ZenDisaster
Snippets were available in Homesite circa 2002. Couldn't they just have copied that? It was way simpler, and just as effective.
- 5/9/2008
- aland7169
I'll second you on that...try MZ-Tools, they have a similar feature which is as simple as copy and paste.
Who ever at Microsoft thought this is an acceptable way to implement such a simple concept has got it all wrong. What a painful way to go about it!
- 12/12/2007
- someduder
- 12/12/2007
- Noelle Mallory
The directions should have been..
1. Click 'File' - 'New' - 'File'
2. Under the New File Dialog Box click 'General' then in the Templates Window click on 'XML File'
OR - if the above isn't available
2. Under the New File Dialog Box click 'Web' - 'C#' then in the Templates Window click on 'XML File'
