Skip to content

Instantly share code, notes, and snippets.

@sethgho
Created April 6, 2012 22:06
Show Gist options
  • Select an option

  • Save sethgho/2323419 to your computer and use it in GitHub Desktop.

Select an option

Save sethgho/2323419 to your computer and use it in GitHub Desktop.
Turn a set of column definitions in SQL into its equivalent XML shredding select clause
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
namespace MasterShredder
{
class Program
{
[STAThread]
static void Main(string[] args)
{
string contents = null;
contents = Clipboard.GetText();
if (string.IsNullOrEmpty(contents))
{
Console.WriteLine("I'm expecting the column definition to be in your clipboard.");
Console.WriteLine("Expected format: [ColumnName] [ColumnType] [NULL or NOT NULL],");
Console.Read();
}
else
{
string[] lines = contents.Split('\n');
StringBuilder result = new StringBuilder();
foreach (string line in lines)
{
//Strip new line and tab characters.
string cleanLine = line.Replace(Environment.NewLine, "").Replace("\t", "");
string[] parts = cleanLine.Split(' ');
if (parts.Length > 2)
{
string name = parts[0].Replace("[", "").Replace("]", "");
string type = parts[1].Replace("[", "").Replace("]", "");
// type definition was split. i.e. decimal(18, 6)
if(parts[2].EndsWith(")"))
{
type += parts[2];
}
string lineResult = String.Format(",a.value('@{0}', '{1}') {0}", name, type);
result.AppendLine(lineResult);
Console.WriteLine(lineResult);
}
}
if(!string.IsNullOrEmpty(result.ToString()))
{
Clipboard.SetText(result.ToString());
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment