public string TransformXML(string xml, string xslt)
{
string output = string.Empty;
XPathDocument xpd = new XPathDocument(new StringReader(xml));
XslCompiledTransform transform = new XslCompiledTransform(true);
transform.Load(new XmlTextReader(xslt, XmlNodeType.Document, null));
StringWriter sr = new StringWriter();
transform.Transform(xpd.CreateNavigator(), null, sr);
output = sr.ToString();
return output;
}
//Note that the XSL needs namespace prefixes to make .Net happy:
xsl = @"<xsl:stylesheet version='1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:output method='text'/>
<xsl:template match='msg'>Found it!</xsl:template>
</xsl:stylesheet>";
xml = @"<msg/>";
Tag Archive for c
Minimalist XSLT Transform Using Two Strings
Singleton
/**
* file Singleton.h
* rief The Singleton design pattern using templates.
* author Justin Chouinard <justin@chouinardconsulting.com>
*
***********************************************************************
* MyClass.h:
***********************************************************************
*
* class MyClass : public Singleton<MyClass>
* {
* ..
* }
*
***********************************************************************
* MyClass.cc
***********************************************************************
*
* MyClass *Singleton<MyClass>::inst = 0;
*
***********************************************************************
* Others
***********************************************************************
* Other files may access the instance by calling MyClass::getInstance().
*
**/
#ifndef __SINGLETON_H__
#define __SINGLETON_H__
template <class Type>
class Singleton
{
public:
static Type *getInstance(void);
private:
static Type *inst;
}; // end of class Singleton
template <class Type>
Type *Singleton<Type>::getInstance(void)
{
if (inst == 0)
inst = new Type;
return inst;
}
template <class Type>
Type *Singleton<Type>::inst = 0;
#endif // __SINGLETON_H__
DataGridView DataBind an Array of Arbitrary Objects
Foo[] foos = new Foo[2];
DataGridView1.DataSource = foos;
//This works.
public class FooGood
{
public string Bar { get; set; }
public string Baz { get; set; }
}
//This Doesn't
public class FooBad
{
public string Bar;
public string Baz;
}
Four Char Code From String in Python
def makeNumberFromCharCode(code):
return (ord(code[0]) << 24) | (ord(code[1]) << 16) | (ord(code[2]) << <img src='http://www.snippetsmania.com/wp-includes/images/smilies/icon_cool.gif' alt='8)' class='wp-smiley' /> | ord(code[3])
# In Carbon / Cocoa:
# FourCharCode code = 'abcd'
# In Python:
# code = makeNumberFromCharCode("abcd")
C# Call Web Service and Get String Result
public string getServiceResult(string serviceUrl) {
HttpWebRequest HttpWReq;
HttpWebResponse HttpWResp;
HttpWReq = (HttpWebRequest)WebRequest.Create(serviceUrl);
HttpWReq.Method = "GET";
HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();
if (HttpWResp.StatusCode == HttpStatusCode.OK)
{
//Consume webservice with basic XML reading, assumes it returns (one) string
XmlReader reader = XmlReader.Create(HttpWResp.GetResponseStream());
while (reader.Read())
{
reader.MoveToFirstAttribute();
if (reader.NodeType == XmlNodeType.Text)
{
return reader.Value;
}
}
return String.Empty;
}
else
{
throw new Exception("Error on remote IP to Country service: "+ HttpWResp.StatusCode.ToString());
}
}
SqlTransaction
SqlConnectionStringBuilder DarthServerConnection = new SqlConnectionStringBuilder();
DarthServerConnection.DataSource = "DarthServer";
DarthServerConnection.InitialCatalog = "DeathStarDB1";
DarthServerConnection.UserID = "tk421";
DarthServerConnection.Password = "tr00p3r";
using ( SqlConnection DeathStarDatabase = new SqlConnection( DarthServerConnection.ToString() ) )
{
// The using statment will call dispose and close the connection.
DeathStarDatabase.Open();
SqlTransaction SaveHeroes = DeathStarDatabase.BeginTransaction();
string ShutDownSql = "UPDATE GarbageDisposal SET Status = 'Shut Down'";
string OpenDoorSql = "UPDATE GarbageDisposal SET Door = 'Open' WHERE GarbageDisposalID = 3263827";
try
{
// Shut down the garbage disposal units on the detention level! No! Shut them all down!
SqlCommand ShutDown = new SqlCommand( ShutDownSql, DeathStarDatabase, SaveHeroes );
ShutDown.ExecuteNonQuery();
// We're alive. Open the door to 3263827.
SqlCommand OpenDoor = new SqlCommand( OpenDoorSql, DeathStarDatabase, SaveHeroes );
OpenDoor.ExecuteNonQuery();
SaveHeroes.Commit();
}
catch ( Exception error )
{
SaveHeroes.Rollback();
MailMessage RescueEmail = new MailMessage( "R2D2@YavinIV.org", "Rebels@YavinIV.org" );
RescueEmail.Subject = "Error while Saving Heroes";
RescueEmail.Body = "An error has occured while attempting to save the following heroes:
"
+ "Luke, Leia, Han, and Chewbacca.
"
+ "The SQL statements that attempted to execute were:
"
+ ShutDownSql + "
"
+ OpenDoorSql + "
"
+ "The error:
"
+ error.ToString() + "
"
+ "Result:
"
+ "The heroes were not saved.
"
+ "Action Item:
"
+ "Please rescue R2D2 and C3PO from the Death Star.";
SmtpClient YavinIVMail = new SmtpClient();
YavinIVMail.Host = "mail.yaviniv.org";
YavinIVMail.Credentials = CredentialCache.DefaultNetworkCredentials;
YavinIVMail.Send( RescueEmail );
}
}
C# String Formats
int MySalary = 123456;
String.Format( "{0:C}", MySalary ); // $123,456.00
String.Format( "{0:D}", MySalary ); // 123456
String.Format( "{0:E}", MySalary ); // 1.234560E+005
String.Format( "{0:F}", MySalary ); // 123456.00
String.Format( "{0:G}", MySalary ); // 123456
String.Format( "{0:N}", MySalary ); // 123,456.00
String.Format( "{0:X}", MySalary ); // 1E240
String.Format( "{0:#%}", MySalary ); // 12345600%
String.Format( "{0:#,###}", MySalary ); // 123,456
String.Format( "{0:#,###.00}", MySalary ); // 123,456.00
String.Format( "{0:#,###.##}", MySalary ); // 123,456
String.Format( "{0:'$'#,###.00}", MySalary ); // $123,456.00
String.Format( "{0:#,###' lbs'}", MySalary ); // 123,456 lbs
MySalary = -123456;
String.Format( "{0:#0}", MySalary ); // -123456
String.Format( "{0:#0;(#0)}", MySalary ); // (123456)
DateTime.Now.ToString( "yyyy-MM-ddTHH:MM:ss" ); // XSD:DateTime format 2009-05-16T10:05:46
C# Reading a File
string DeathStarPlans = @"C:DeathStarPlans.txt";
StringBuilder R2D2 = new StringBuilder();
using ( StreamReader PrincessLeia = new StreamReader( DeathStarPlans ) )
{
while ( PrincessLeia.Peek() >= 0 )
{
R2D2.Append( PrincessLeia.ReadLine() );
}
}
Formatting DateTime
using System;
using System.Collections.Generic;
using System.Text;
namespace DateTimeParse
{
class DateConv
{
static void Main(string[] args)
{
// Current Date/Time
Console.WriteLine("Using Now Property --> " + DateTime.Now);
Console.WriteLine("Using Today Property --> " + DateTime.Today);
// DateTime in a text format
string datetimeString = "6/2/2006 12:00:00 PM";
Console.WriteLine("Text Formatted Date and Time --> " + datetimeString);
// Convert the text DateTime to a DateTime format
// This is just to show the syntax involved in a text to DateTime conversion
DateTime convertedDateTime = DateTime.Parse(datetimeString);
Console.WriteLine("Converted to DateTime --> " + convertedDateTime);
// Extract just the date portion of the DateTime variable
string justDate = convertedDateTime.Date.ToShortDateString();
Console.WriteLine("Extract Date Only: --> " + justDate);
// Once the variable is in DateTime format - several different variations
Console.WriteLine("d: {0:d}", convertedDateTime);
Console.WriteLine("D: {0:D}", convertedDateTime);
Console.WriteLine("f: {0:f}", convertedDateTime);
Console.WriteLine("F: {0:F}", convertedDateTime);
Console.WriteLine("g: {0:g}", convertedDateTime);
Console.WriteLine("G: {0:G}", convertedDateTime);
Console.WriteLine("m: {0:m}", convertedDateTime);
Console.WriteLine("M: {0:M}", convertedDateTime);
Console.WriteLine("r: {0:r}", convertedDateTime);
Console.WriteLine("R: {0:R}", convertedDateTime);
Console.WriteLine("s: {0:s}", convertedDateTime);
Console.WriteLine("t: {0:t}", convertedDateTime);
Console.WriteLine("T: {0:T}", convertedDateTime);
Console.WriteLine("u: {0:u}", convertedDateTime);
Console.WriteLine("U: {0:U}", convertedDateTime);
Console.WriteLine("y: {0:y}", convertedDateTime);
Console.WriteLine("Y: {0:Y}", convertedDateTime);
}
}
}
Convert ASP.NET PlaceHolder to HTML tag with attributes
public static void PlaceHolderToTag(PlaceHolder holder, string tag, HtmlAttr[] attributes) {
if (!holder.Visible) { return; }
//Init new control to use as tag
HtmlControl newControl = new HtmlGenericControl(tag);
//Add attributes
foreach (HtmlAttr attr in attributes) {
newControl.Attributes.Add(attr.Key, attr.Value);
}
//Add all the place holder's controls
foreach (Control ctrl in holder.Controls) {
newControl.Controls.Add(ctrl);
}
holder.Parent.Controls.Add(newControl);
holder.Controls.Clear();
}