Tech Blog

Jay's Technical blog

Serializing DataSets

30 March 2004
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

When you serialize a dataset, an XML document is created containing the data (everyone knows this, right?)  I just recently did some investigation on the string that gets created for a serialized dataset... Dang!  It contains a lot more info than I thought.  The reason is that it contains the XML Schema plus the data that is contained in the rows.  For copying the data from a query into session, viewstate, or the cache (so that you can re-use the data later), this is way overkill. 

So I had already come up with a better way (doing some caching of data via files).  The dataset also has two methods called ReadXML and WriteXML.  You can use these to create an XML string that contains simply the data.  So here's the code (sorry for the lack of vb colors):

Option Strict On

Imports System.Xml
Imports System.Data
Imports System.Text

Public Class DataSet_Serializaion
  Public Shared Function SmartSerialize(ByVal ds As DataSet) As String
    Dim __ms As System.IO.MemoryStream
    Dim __dcod As Decoder
    Dim __chars() As Char
    Try

      __ms = New System.IO.MemoryStream
      __dcod = Encoding.UTF8.GetDecoder()
      ds.WriteXml(__ms)

      ReDim __chars(CInt(__ms.Length))

      __dcod.GetChars(__ms.GetBuffer(), 0, CInt(__ms.Length), __chars, 0)
      Return New String(__chars)
    Finally
      If Not (__ms Is Nothing) Then
        __ms.Close()
        __ms = Nothing
      End If
      __dcod = Nothing
    End Try

  End Function

  Public Shared Function SmartDeSerialize(ByVal serialized As String) As DataSet
    Dim __ds As DataSet
    ' needed to create an XML TextReader 
    Dim nt As NameTable
    Dim nsmgr As XmlNamespaceManager
    Dim context As XmlParserContext
    Dim tr As XmlTextReader
    Try
      __ds = New DataSet
      nt = New NameTable
      nsmgr = New XmlNamespaceManager(nt)
      context = New XmlParserContext(Nothing, nsmgr, Nothing, XmlSpace.None)

      tr = New XmlTextReader(serialized, XmlNodeType.Document, context)
      __ds.ReadXml(tr)

      Return __ds
    Finally
      If Not (tr Is Nothing) Then
        tr.Close()
        tr = Nothing
      End If
      context = Nothing
      nsmgr = Nothing
      nt = Nothing

    End Try

  End Function
End Class

(CODE NOTE: I had problems using the memory stream for input so I used a generic textreader instead).

Anyway, this will produce an XML representation of just the data behind the dataset.  I would say that you would want to use this one for datasets containing data with no relationships.  I would avoid the use of the above method for data that you are updating (in other words, don't use this for datasets that you intend to use for batch updates).

This example works pretty good... I would still like to add some of the stuff that Carl Franklin's did in his CarlsZipLibrary located here.

(Wow, post #6... I'm almost somebody.. hahaha)


Jumping on the bandwagon...

25 March 2004
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

This week I have been experimenting with Test-Driven development.  When I can get myself out of my old habits I think I'm going to be very proficient with this.  I find myself fixing issues long before I've left the initial development of the code.

This is awesome!

If you don't know what I'm talking about, the latest issue of MSDN magazine has an article on it - http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/aspnet-testwithnunit.asp

I should mention that there is also a framework for working with asp.net (NunitASP) -- which I'm using.  If you haven't checked this out you should!  I'm not sure where I stand with the rest of the extreme programming techniques, but I definitely like test driven development...

I truly agree that quality will be a measuring point for us all in the not-too distant future.


What I don't like about DPAPI...

24 March 2004
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

I know that right after Dev Days right now there is a big buzz about DPAPI.  I think the main reasons is that it really simplifies cryptography to a point that we can essentially ignore key generation (at least this appears to be the case for every example that I haven't seen).  Key generation is defined by looking at when you want the data to be able to be decrypt (do I tie this data to the user, or to this machine, for instance).

I know that the goal here is to make it easier for “Joe Programmer“ (I hate the term “Mort“) to use encryption without having to know a lot about cryptography and the cryptography framework.

Here are my problems with DPAPI.  First of all it uses Triple-DES which is not the absolute strongest encryption (AES/Rijn-Dael is actually stronger).  I wish that we could determine behind the scenes how the strong the encryption is (someone told me that this is possible, but it is not easy to do). 

Sometimes you want to go the opposite direction and munge the data so prying eyes (of users) can't tell what it is, but that the programmer can mentally decrypt (building a simple ROT13 encryption scheme for example would accomplish this); this would let us munge the data settings in our app.config files, for instance.

There are times when I want to control the key (because I need the data to be decrypted on machines or by the people that I say should be able to decrypt it).  DPAPI actually doesn't let me set the key (at least as far as I can tell), so I give up some of the control.

Something else, I would like to see MS do is give us an application block of something similar that lets us securely retrieve/store our crypto keys on a machine.  Maybe they should strip the key generation features (maybe they have) out and let me just use them so I can use this with my own encryption.


ASP/vbscript Codebehind

20 March 2004
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

I'm one of the unlucky ones who has to still deal with old ASP.  In my day job, I work on a site that is for the most part pure ASP.  I've developed a pretty cool technique to make me forget that I'm using Asp and not ASP.NET (at least I think it is cool).  I think the technique should make it easy to convert a page over to ASP.Net.

As you may or may not remember Microsoft added class support to VBScript (in one of the last iterations I think), so you can create classes in ASP.  Since I'm really in love with what codebehind gives us in ASP.Net (namely that VB code and HTML code are separated), I decided to build myself a methodology (and in my case it's not too strict of one) for doing ASP.

So I have the main asp page that looks like this (in green)

<!--#include file=”codebehind/myPage_codebehind.asp”-->
<p><% page.callSomeMethid%></p>
<%
Set page = Nothing%>

In the same directory as this page I have subdirectory called “codebehind” and in this directory I have another page called somehting like thispagename_codebhind.asp.  This page looks something like this (in blue)

<!-- any additional includes here -->
<%
Class myPage

  Public Sub SomeMethod()
    ' Does nothing
  End Sub

  ' Real Events
  Private Sub class_initialize()
     ' Initialization code goes here (as well as catches of postback)

     ' I have also built mock asp.net events here (these are simply additional methods that I call in order from here)
  End Sub

  Private Sub class_terminate()
     ' Close down stuff created (I could for instance keep 1 connection object throughout the entire page and close it here
  End Sub
End Class


' Instantiate the page
Dim page
Set page = New myPage
%>


Anyway, this has the desired affect of pretty much splitting the HTML code from the VBScript code.  My suspicion is that VBScript classes may be a little slower than straight code in the page, but if you plan on doing a conversion over to ASP.Net then the cost in performance for a few months may be something worthwhile (especially when you are in my shoes where I spent a long time trying to convince my boss to let me do the conversion...

BTW, I'm real now I posted 1 technology item.  You can now count me as a real blogger (seethis page for details) 

Now I can disappear and not feel like I have a wasted blog...


Definition of a Dev Theologian

20 March 2004
Jay Kimble

[WARNING! This is an archived post and as such there may be things broken/missing here.. you have been warned.]

Alright, I'm sure if you're one of the couple people I have invited to my blog, you may or may not be wondering what a “Dev Theologian” is.

Blame Joe Healy
Blame Joe Healy of Microsoft for it.  He inadvertantly gave it to me.  Joe is the .Net Evangelist for the local region here in Florida.

It's a rather long story that I won't go heavily into, but in the course of an email conversation, I mentioned to Joe that I have a degree in Theology (actually the official title of my Degree is “B.A. in Christian Ministries”).  Joe told me that I was the first Developer Theologian that he had ever met.  I liked the name so I'm using it...

So what are you going to do here?
I want to do a comparative between the writings of St. Paul and VB.Net.  NOT!!!! 

Actually, I use my degree, but it will probably never make me a dime in the religious establishment.  Think of my degree more as a Philosophy degree with an emphasis in writing (and ancient books).

So what am I going to talk about in this Blog?  Let me start by saying that I love to code, I love new technology, and I love to look at new ways to use technology.  So I'm just like everyone else.  If anything I'm a mad coder... I have a knack for coming up with unusual ways of using technology (some of which are useful in the right context).  I will probably explore some of the philosophical aspects of us developers.  I'll also rant from time to time (thank you Rory Blythe for showing me how to rant and be funny about it).