C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
 C# Code Snippets  C# Code Snippets
Showing posts with label Xml. Show all posts
Showing posts with label Xml. Show all posts

Friday, December 14, 2007

How To Create Code Snippets Easily

Hi all

Whenever I want to create a code snippet I usually take an already created one and change the XML file to suit my needs, well NO MORE!!!!

introducing Snippy the Code snippet editor, creating code snippets was never so Easy!!!

let's check out the tool with a snippet I wrote for a class with one variable including a constructor and a property for that variable.

snippy

It's so easy, you just give it a shortcut to use in Visual Studio, add the literals you want to use and then write the code you need using the literals. When you are done just save it in your Visual Studio Custom Snippets folder, which is usually : "C:\Documents and Settings\<USERNAME>\My Documents\Visual Studio 2008\Code Snippets\Visual C#\My Code Snippets", and that's it. Next time you run Visual Studio type in the shortcut and let the magic begin... :)

the out put of this snippet is this:

<?xml version="1.0" encoding="utf-8"?>
<
CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
<
CodeSnippet Format="1.0.0">
<
Header>
<
SnippetTypes>
<
SnippetType>Expansion</SnippetType>
</
SnippetTypes>
<
Title>Class_1</Title>
<
Shortcut>Cls1</Shortcut>
<
Description>template for class with one variable</Description>
<
Author>Amit Raz</Author>
</
Header>
<
Snippet>
<
Declarations>
<
Literal Editable="true">
<
ID>Member1</ID>
<
ToolTip>First member</ToolTip>
<
Default>member</Default>
<
Function>
</
Function>
</
Literal>
<
Literal Editable="true">
<
ID>Type1</ID>
<
ToolTip>type 1</ToolTip>
<
Default>int</Default>
<
Function>
</
Function>
</
Literal>
<
Literal Editable="true">
<
ID>MemberPropName</ID>
<
ToolTip>
</
ToolTip>
<
Default>propname</Default>
<
Function>
</
Function>
</
Literal>
<
Object Editable="true">
<
ID>ClassName</ID>
<
ToolTip>Class name</ToolTip>
<
Default>class1</Default>
<
Function>
</
Function>
</
Object>
</
Declarations>
<
Code Language="csharp"><![CDATA[public class $ClassName$
{
private $Type1$ $Member1$;

public $Type1$ $MemberPropName$
{
get
{
return $Member1$;
}
}

public $ClassName$($Type1$ newMember)
{
$Member1$ = newMember;
}
}
]]></Code>
</
Snippet>
</
CodeSnippet>
</
CodeSnippets>


Not very friendly right? let's just have snippy do all the work...



You can download it here:



http://www.gotdotnet.com/codegallery/codegallery.aspx?id=b0813ae7-466a-43c2-b2ad-f87e4ee6bc39



hope you Enjoy this



Amit

AddThis Social Bookmark Button

Wednesday, December 12, 2007

How to add a new XML node to file

Need to open an XML file and add a node?

Here's how:

Sample XML file:

<?xml version="1.0"?>
<
HelpData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<
helpButtonUrls>
<
HelpButtonUrl>
<
buttonName>BugReport</buttonName>
<
url>C:\BugRep.exe</url>
</
HelpButtonUrl>
</
helpButtonUrls>
</
HelpData>

We want to add another HelpButtonNode:

private void AddNodeToXMLFile(string XmlFilePath, string NodeNameToAddTo)
{
//create new instance of XmlDocument
XmlDocument doc = new XmlDocument();

//load from file
doc.Load(XmlFilePath);

//create main node
XmlNode node = doc.CreateNode(XmlNodeType.Element, "HelpButtonUrl", null);

//create the nodes first child
XmlNode ButtonName = doc.CreateElement("buttonName");
//set the value
ButtonName.InnerText = "Video Help";

//create the nodes second child
XmlNode url = doc.CreateElement("url");
//set the value
url.InnerText = "D:\\RunHelp.exe";

// add childes to father
node.AppendChild(ButtonName);
node.AppendChild(url);

// find the node we want to add the new node to
XmlNodeList l = doc.GetElementsByTagName(NodeNameToAddTo);
// append the new node
l[0].AppendChild(node);
// save the file
doc.Save(XmlFilePath);
}


XML file after:


<?xml version="1.0"?>
<
HelpData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<
helpButtonUrls>
<
HelpButtonUrl>
<
buttonName>BugReport</buttonName>
<
url>C:\BugRep.exe</url>
</
HelpButtonUrl>
<
HelpButtonUrl>
<
buttonName>Video Help</buttonName>
<
url>D:\RunHelp.exe</url>
</
HelpButtonUrl>
</
helpButtonUrls>
</
HelpData>




And thats all there is to it!



AddThis Social Bookmark Button