Posted by Dan Rigsby on 21st December 2007
There are many times when I have xml and xslt as strings and need the transformed data as a string. I put together a utility class to take care of this.
/// <summary>
/// Transforms the specified xml string using the specified xslt string.
/// </summary>
/// <param name=”xml”>The xml string to transform.</param>
/// <param name=”xslt”>The xslt string to transform with.</param>
/// <returns>The transformed xml string.</returns>
public static string Transform(
string xml,
string xslt
)
{
string output = String.Empty;
StringWriter stringWriter = null;
XmlTextWriter xmlWriter = null;
XmlDocument inputDocument = new XmlDocument();
XmlDocument xsltDocument = new XmlDocument();
System.Xml.Xsl.XslCompiledTransform xslCompiledTransformer = null;
try
{
// Create Xml Document
inputDocument.LoadXml(xml);
// Create XsltDocument
xsltDocument.LoadXml(xslt);
stringWriter = new StringWriter();
xmlWriter = new XmlTextWriter(stringWriter);
// Apply the generated text stylesheet.
#if DEBUG
xslCompiledTransformer =
new System.Xml.Xsl.XslCompiledTransform(true);
#else
xslCompiledTransformer =
new System.Xml.Xsl.XslCompiledTransform(false);
#endif
xslCompiledTransformer.Load(xsltDocument);
xslCompiledTransformer.Transform(inputDocument, xmlWriter);
// Generate the output Xml
output = stringWriter.GetStringBuilder().ToString();
}
finally
{
if (xmlWriter != null)
{
xmlWriter.Close();
}
if (stringWriter != null)
{
stringWriter.Dispose();
}
if (xslCompiledTransformer != null)
{
xslCompiledTransformer.TemporaryFiles.Delete();
}
}
return output;
}
A couple of things of note:
- When I first started using this, I had this running in memory and it would transform over 100 times an hour. We had a variety of stylesheets, and the xml was almost always different. If we didn’t make a call to TemporaryFiles.Delete(), then these temporary files would be kept and overtime this resulted in a lot of useless files. This seems like a bug to me that this files aren’t cleaned up by default, but I am sure there is some clever reason for this. It may depend on how you are implementing it. I do like having the flexibility of deciding whether or not to keep them though. You can read more about these options on the MSDN page for XsltCompiledTransform.
- I had thought of making the XsltCompiledTransform a static variable, but depending on how this method is used, I didn’t want to always have this loaded in memory. If you make use of this code, you may decide to do this.
Posted in .Net, xslt | No Comments »
Posted by Dan Rigsby on 21st December 2007
When creating a connection string it can be hard to remember all of the parameters to use. You could use a resource like http://www.connectionstrings.com/, but even then its hard to simply test if it will work or not. What if you have all of the connection information, but have forgotten the database name?
An easy way to solve all of this is to use a Microsoft Data Link file. This is a file with the .udl extension. You can create a file on your desktop or in a folder and just give it a name like "a.udl". (The file itself doesn’t need to contain anything to be opened up in the data link properties window.) Then, just double click on the file and will open up in the data link properties window like this:

You can now fill in the data you know and test connection at any time. When you are done, just open up the .udl file in a text editor like notepad and it will contain the actual connection string.

Posted in Database | No Comments »
Posted by Dan Rigsby on 19th December 2007
I finally got around to publishing an article on codeproject. I chose the ReadOnlyController as its a fairly simple concept, is an excellent demonstration of how to use the IExtenderProvider, and would be a small enough project to get up on codeproject and learn the ropes. I have created projects on codeplex before. Codeplex is very nice, but is meant for projects. Single components like the ReadOnlyController are better suited for codeproject.
The ReadOnlyController is a Component so that can be dropped on a form in design mode and implements IExtenderProvider so that it can extend the properties on controls on the form. Controls that have either a Readonly or Enabled property can be extended to enable control from the ReadOnly Controller. See the screenshot below for how this might look in the designer:

Link:
http://www.codeproject.com/KB/cs/ReadOnlyController.aspx
Posted in .Net, Common Libraries | No Comments »
Posted by Dan Rigsby on 16th December 2007
I ran into an issue earlier this week while working with a generic class where one of the template parameters would represent a possible enum value. (Basically we have different enum lists representing different actions). You cant use a “where” clause for the enum class. So how would take a string or an int and cast it back to the type of enum defined in the template parameter? I had thought the static Enum class would provide new methods such as Enum.ToObject<T> and Enum.Parse<T> to match the non-generic methods, but they weren’t there. My needs here were pretty specific, but I soon realized that every time I cast an into to an enum I had to cast the result of Enum.ToObject(int) to the enum type. Why wouldn’t they have implemented generic methods for these common task? Generics were built for stuff like this.
Here is a basic EnumUtilities class I built to work around this issue and to assist with enum casting in the future:
using System;
namespace ININ.Common
{
/// <summary>
/// Utilities to assist with enums.
/// </summary>
public static class EnumUtilities
{
/// <summary>
/// Parses the enum.
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”name”>The name.</param>
/// <returns></returns>
public static T ParseEnum<T>(
string name)
{
if (String.IsNullOrEmpty(name))
{
throw new NullArguementException(“name is null or an empty string.”);
}
return (T)Enum.Parse(typeof(T), name);
}
/// <summary>
/// Parses the enum ID to the enum type.
/// </summary>
/// <typeparam name=”T”></typeparam>
/// <param name=”i”>The i.</param>
/// <returns></returns>
public static T ToObject<T>(
int i)
{
return (T)Enum.ToObject(typeof(T), i);
}
}
}
Posted in .Net, Common Libraries | No Comments »
Posted by Dan Rigsby on 15th December 2007
Last night we had an Installfest here in Indianapolis for Visual Studio 2008. This event has been in the works for a couple of months now and was made possible by our Friends and Microsoft as sort of a "mini" release event to show their appreciation for the .Net user’s groups across the country. This took the place of our normal monthly .Net User’s meeting, but was a welcomed diversion from the standard "pizza and a speaker" format. The evening played out as followed:
- The Setup - As an officer for the user’s group, I arrived early to volunteer my time helping to set things up. I spent a lot of time making sure Guitar Hero 3 was working correctly for all the members in attendance. I know they appreciated all the time I spent testing all of the songs to make sure the disk wasn’t corrupt.
- The Sign-In - Not all of the people who registered attended, but we did have over 150 in attendance and over a third of those brought gifts for the RVT6 Toy Drive.
- The Wait - We were given copies of the CD at the door and a lot of people had begun the installs. We ran cables and power strips down every isle of chairs to provider power for the attendees. Food was serviced, but we were told to wait until the event began.
- Introduction - InstallFest finally began with Brad Jones and Dave Bost going over the format format for the evening and introducing Visual Studio 2008.
- Installs and Play Time - The major of the evening was spent eating, installing, playing games, and networking. I ran into many old friends and spent a good portion of the evening talking.
- Mini-Demos - We were asked to present mini demos of features in Visual Studio 2008 or .Net 3.5. Unfortunately I didn’t present a topic as I have been pretty busy with a project for school. However we had a hand full of people present including two of my friends from work: Aaron Lerch and Jeff Moser. Jeff ending up winning a Zune for his efforts, Congrats!
- Prizes - There were numerous prizes including 2 XBoxes, 3 Zunes, lots of games, and a pile of books. Amazingly I didn’t win anything again. That is a trend, but congrats to all of the winners!
Dave Bost made it down the for the event, but Larry Clarkin missed out again. Overall it was very enjoyable and I can’t wait for other events in the future.
Posted in .Net, Community | 2 Comments »