Quick tip: Using string interpolation in XML code snippet files

Took me a few minutes to figure out and thought I would share how to use string interpolation in your XML code snippets files.

Simply put, add the Delimiter attribute to your Code element and set it to a value other than the default of $. I used % in this example. This allows me to use $ freely as literal text.

C# 6.0 compatible.

<CodeSnippet Format="1.0.0"> <Header> <Title>Log Verbose</Title> <Author>Mr. Coma</Author> <Shortcut>logVerbose</Shortcut> <Description>Snippet for adding a Log Verbose statement.</Description> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> </Header> <Snippet> <Declarations> <Literal default="true"> <ID>messageFormat</ID> <ToolTip>Replace with your message format string. </ToolTip> <Default>message</Default> </Literal> <Literal default="true"> <ID>formatParameter</ID> <ToolTip>Replace with your message format parameter values.</ToolTip> <Default>value</Default> </Literal> </Declarations> <Code Language="csharp" Kind="method body" Delimiter="%"> <![CDATA[Log.Write(Log.Level.Verbose, $"%messageFormat%"); ]]> </Code> </Snippet> </CodeSnippet> 

by sausagecoma via /r/csharp

Leave a Reply