Wednesday, March 14, 2007

xsd Command line tool for Visual Studio

There is a perhaps quite underutilized command line tool available for Visual Studio. It is called xsd.exe and is installed in most typical configurations of Visual Studio 2005 (and 2003 for that matter). To use it, simply open a Visual Studio 2005 Command Prompt window (from the Visual Studio -> Visual Studio Tools folder in the start menu). Alternatively you can open the folder C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin substituting your custom install path if necessary.

It is capable of a number of quite useful operations including:

1) The ability to generate XSD schema files from XML files. The syntax for that is simply:
xsd %1
Where %1 is the name of the xml file. This will generate an xsd with the same filename as the xml except with an xsd extension.

2) You can generate sub classed datasets via this method:
xsd %1 /d
Where %1 is the xsd file. This will output similary named class files that provide custom datasets based on the schema provided. The smart bit about this is that hidden primary and foreign keys are added to the dataset transparently. The reason why this is so smart is that is provides a means for you to programatically link nested elements to parent elements. When a typical command like:
MyDataSet.WriteXML(@"c:\out.xml"); or
MyDataSet.GetXML();
the Xml generated adheres to the schema provided during your xsd output. For some of you this may be pretty straight forward but I know some, including myself, who it took a while to figure out how this all fits together.

3) You can also generate classes for the objects, which is probably less useful, but handy for those wanting assistance to quickly create business objects for large schemas. The way to do this is:
xsd %1 /c
The %1 parameter being the xsd schema file.

4) Another useful operation for this command line utility is the ability to convert xdr to xsd. This can sometimes save a lot of effort! To do this:
xsd %1
The parameter %1 being the name of the xdr file (which must have an .xdr extension!).

Note that by default the classes (and dataset classes) that are generated output as c #. You can alter which language these output in by using the /language (or /l) switch. The possibilities are CS (C#), VB, JS, VJS or CPP. You can also instead "provide a name for a fully qualified class implementing System.CodeDom.Compiler.CodeDomProvider". This will let you output in your favourite niche langauge (anyone for IronPython or perhaps even f#?).

There are a bunch of other cool things this utility can achieve but I shall leave the exploration to you.

ASP.NET Label Control Aligning Text

A fellow developer at work today was struggling to find away to perform what should be a fairly simple task. He was attempting to align some text in an asp:label control to the right. As their is no extremely obvious align like property some confusion resulted. After a quick bit of research and trial and error we worked it out.

1) Make sure the asp:label has a fixed width. This way aligning to the right is actually meaningful.

2) In your page_load or other relevant code section insert a piece of code similar to the following:
Label1.Style.Add("text-align", "right");

note that this can also be accessed like so:
Label1.Attributes.CssStyle.Add("text-align", "right");

The attributes element is also worth exploring for your own research, it can provide an enumerator for all keys associated with the current control.