Scott Hanselman

Team Hanselman and Diabetes Walk 2006

March 20, 2006 Comment on this post [5] Posted in Diabetes | DasBlog
Sponsored By

BannerLogoUPDATE: For those who couldn't find it, here's the link to make a secure Donation with your Credit Card. In the US, these are Tax-Deductible.

As of 10pm on 3/23, we are 83% of the way there, so if you've been waiting until now to make yourself known, this is a great time to help push us over the edge. I am truly humbled by the outpouring of support. You guys and gals are wonderful and you really make this blog what it is. I loved blogging before, but seeing how you've all come out in full force just underscores not only the power of blogging, but the level of kindness present in the blogging community. Thanks for reading!  You can always see our dynamic updated status here.

UPDATE #2: If you'd like a banner for your blog to display until the walk on May 6th, feel free use this one and link it directly to the ADA here.


This is a technical blog, but I'm not just a technical person full of source code and pomposity.

Two months before my 21st birthday I started peeing a lot. A LOT. Like I was drinking four 2-liter bottles of Sprite a day and was still thirsty beyond belief. We'd just had a family photo taken and I was 130lbs on a 5'11" frame (for those of you outside the US, that's thin.) I was wasting away and looked like death. My father, a Portland Firefighter and Paramedic for thirty years smelled the sugar on my breath and sent me right away to the hospital where my blood glucose level was higher than the meter could read...and it's supposed to be under 100mg/dl.

I spent that spring learning how to give myself shots, four a day, along with a regiment of pills. Twelve years later I have no side effects, knock on wood. Not everyone is that lucky. I recently went to a funeral of a high-school friend who was the exact same age and succumbed to Type 1 Diabetes.

I take three shots a day of Symlin while also wearing an Insulin Pump 24-hours a day, even while I sleep. The pump saves me from an additional six shots a day, which I took for 8 years before the pump. I test my blood sugar by pricking my finger between 8 and 10 times a day - that's about 43,000 finger pricks so far, and miles to go before I sleep.

I consider myself lucky though. My 90-year old grandmother's neighbor friend in the 1920's, before Insulin was widely used (it was discovered in 1921) ate nothing but lettuce and eventually died in childhood. I have friends who have been diabetic for nearly 50 years and had to boil large-gauge needles on the stove before injecting themselves with Pork-derived insulin, basing their decisions on a once-a-day urine check to check their blood glucose level.

Diabetes is endemic. Here's some stats from the NIH:

  • Total: 20.8 million people—7 percent of the population—have diabetes.
    • Diagnosed: 14.6 million people
    • Un-diagnosed: 6.2 million people
  • 1.5 million new cases of diabetes were diagnosed in people aged 20 years or older in 2005.
    • Diabetes was the sixth leading cause of death listed on U.S. death certificates in 2002.
  • Diabetes is the leading cause of new cases of blindness among adults aged 20 to 74 years.
  • Diabetes is the leading cause of kidney failure, accounting for 44 percent of new cases in 2002.
  • About 60 to 70 percent of people with diabetes have mild to severe forms of nervous system damage.

I tell you this not to scare you, or ask for pity. I tell you this because it's the painful truth. It sucks, and it sucks big time. 

This year Team Hanselman, led by myself and my wife, Mo, who had this whole idea, will be walking to fight diabetes on May 6th, 2006. We have set a goal of raising US$10,000. Crazy, huh?

Well, there are over 5000 people who subscribe to this blog (for the technical content, I assume.)

If only 10% of you, dear readers, gave US$20 to this cause, we've met our Team Goal.

If you aren't familiar with Diabetes, perhaps my explanation and the above statistics will help you understand how personally painful this disease is.

Perhaps you've Googled and found my blog useful in the past or you've seen me speak at a conference or local user's group. Maybe you're a blogger yourself and use DasBlog.

If you've ever thought about giving a 'tip' to this blog, here's your chance to make that tip tax-deductible! (if you're in the US) You can also paypal me your donation and I will personally deliver 100% of your money.

  • Pass around the link to this post to your friends, your family. Email it to your company's internal email alias. Show us the power of blogging, of word of mouth and spread the word.

Thanks for your patient attention, we now return you to ComputerZen.com.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Not quite enumerating (iterating) enums

March 19, 2006 Comment on this post [9] Posted in ASP.NET | Internationalization | XML
Sponsored By

Warning/Disclaimer: This post is very likely useless and random, providing neither prescriptive guidance nor valuable suggestion. These are the ramblings of an idiot with possibly low blood sugar. There are at LEAST a dozen ways to write this silly thing. This is a largely academic post, as the end result doesn't affect the product fundamentally and the code in question only runs at startup so questions of perf is meaningless. End of speech.

Given:

I've got this giant XSD (XML Schema) with a pile of enumerations that represent languages. No, I don't own the XSD, it's a small part of a giant specification. I don't want to modify the schema.

I generated a giant C# from file from this giant schema via XSD.exe. I don't want to modify the generated code. (Random aside, when I generate code, I like to name the files *.g.cs to make it clear.)

The enum looks like this (there's 454 of them, FYI)...

public enum LanguageEnum {

 

    AAR,

    ABK,

    ACE,

    ACH,

    //snip.... 

    ZUL,

    ZUN,

}

These are the ISO 639.1 3-letter Language Codes. These are going to be in an XML Document that will be HTTP POST'ed to a URI endpoint. I want to 'convert' them to a System.Globalization.CultureInfo via a mapping mechanism and set the CultureInfo on the current thread.

The names of the enums are important, not any implied underlying value. (They don't map to ints, etc)

The constructor for System.Globalization.CultureInfo takes a string like en-us (the ISO 639-1 Alpha 2, not these Alpha 3 codes. Restated, they want "en" or "en-us" not "ENG." However, this info IS inside of CultureInfo.ThreeLetterISOLanguageName.

I'd like clean mapping, but don't feel like writing it manually.

I wrote it this way first in a fit about 90 seconds long. Note the weird try catch. Note also that this works (i.e. achieves the goal).

(Aside,I write a lot of methods like this that lazy-initialize against a hashtable of schmutz, so this is a common pattern for me.)

private static volatile Hashtable iso639toCultureInfo = null;

private static object hashtableLock = new object();

 

public void SetThreadCulture(LanguageEnum foolanguage)

{

    //Get a hashtable that maps the ISO639 three letter name to Windows Cultures

    if(iso639toCultureInfo == null)

    {

        lock(hashtableLock)

        {

            if (iso639toCultureInfo == null)

            {

                iso639toCultureInfo = new Hashtable();

                foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.NeutralCultures))

                {

                    string potentialFooLanguage = ci.ThreeLetterISOLanguageName;

                    try

                    {

                        //May throw an exception because FOO doesn't support this language

                        LanguageEnum lang = (LanguageEnum)Enum.Parse(typeof(LanguageEnum),potentialFooLanguage,true);

                        //May ALSO throw an exception because we already added it

                        iso639toCultureInfo.Add(lang.ToString(),ci);

                    }

                    catch (Exception)

                    {

                        ;

                    }

                }

            }

        }

    }

 

    CultureInfo fooCulture = iso639toCultureInfo[foolanguage.ToString()] as CultureInfo;

    if(fooCulture != null)

    {

        System.Threading.Thread.CurrentThread.CurrentCulture =
           System.Threading.Thread.CurrentThread.CurrentUICulture = 
              fooCulture;

    }

}

All the Unit Tests passed, blah blah, then I had a change of heart and did this about 30 seconds later:

    private static volatile Hashtable iso639toCultureInfo = null;

    private static object hashtableLock = new object();

 

    public void SetThreadCulture(LanguageEnum foolanguage)

    {

        //Get a hashtable that maps the ISO639 three letter name to Windows Cultures

        if(iso639toCultureInfo == null)

        {

            lock(hashtableLock)

            {

                if (iso639toCultureInfo == null)

                {

                    iso639toCultureInfo = new Hashtable();

                    StringCollection languages = new StringCollection();

                    languages.AddRange(Enum.GetNames(typeof(LanguageEnum)));

 

                    foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.NeutralCultures))

                    {

                        string potentialFooLanguage = ci.ThreeLetterISOLanguageName.ToUpper();

                        if(languages.Contains(potentialFooLanguage))

                        {

                            iso639toCultureInfo.Add(potentialFooLanguage,ci);

                        }

                    }

                }

            }

        }

 

        CultureInfo fooCulture = iso639toCultureInfo[foolanguage.ToString()] as CultureInfo;

        if(fooCulture != null)

        {

            System.Threading.Thread.CurrentThread.CurrentCulture =
               System.Threading.Thread.CurrentThread.CurrentUICulture =
                  fooCulture;

        }

    }

Which way do you prefer? What would you have done?

Incidentally, of these 434 (largely obscure) languages in the enum, .NET/Windows 'supports' 50 of them as neutral cultures. Also, the enum has "FRE" for France, while the code in CultureInfo is "FRA" so I ended up changing the enum anyway. Sigh.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Microsoft Fingerprint Reader and Password Minder

March 19, 2006 Comment on this post [6] Posted in ASP.NET | XML | Tools
Sponsored By

FingerPrintPasswordMinderI love my Microsoft Fingerprint Reader, and I use it all the time at home. I also love Keith Brown's Password Minder, it's where I keep all my secret stuff. I also keep the pa ssword file synchronized using FolderShare.

While the software that comes with the Fingerprint Reader will also store passwords, marrying them with your fingerprint, I like the idea of keeping my passwords in the pwd.xml file - it's more mobile and sync's nicely. I use the fingerprint login for a number of sites, but I keep my financial passwords super complex and in the Password Minder.

I never realized - doh! - that the Fingerprint Reader not only managed passwords for Web Sites, but Windows Programs as well.

This makes it twice as useful as I thought it was, and that was pretty useful.

Of course, the next obvious thing was to combine the two, hence the image at right. They work great together. Might seem stupid to use one password secret store to open another, but I thought it was cool. Now maybe I'll put them all on a TrueCrypt drive...mwahahahaha

Now playing: Audrey Niffenegger - The Time Traveler's Wife, Part 1

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Hanselminutes Podcast 10

March 15, 2006 Comment on this post [4] Posted in Podcast | ASP.NET | Watir | Ruby | XML | Tools
Sponsored By

HanselminutesMy tenth Podcast is up. This episode is about (practical) functional web testing, including Watir, FireWatir, SilkTest, WinRunner, TestComplete, Selenium and a dozen others.

We're listed in the iTunes Podcast Directory, so I encourage you to subscribe with a single click (two in Firefox) with the button below. For those of you on slower connections there are lo-fi and torrent-based versions as well.

Subscribe to my Podcast in iTunes

Our sponsors are Automated QA, PeterBlum and the .NET Dev Journal.

As I've said before this show comes to you with the audio expertise and stewardship of Carl Franklin. The name comes from Travis Illig, but the goal of the show is simple. Avoid wasting the listener's time. (and make the commute less boring)

  • Each show will include a number of links, and all those links will be posted along with the show on the site. There were 16 sites mentioned in this tenth episode, some planned, some not. We're still using Shrinkster.com on this show.
  • The basic MP3 feed is here, and the iPod friendly one is here. There's a number of other ways you can get it (streaming, straight download, etc) that are all up on the site just below the fold. I use iTunes, myself, to listen to most podcasts, but I also use FeedDemon and it's built in support.
  • Note that for now, because of bandwidth constraints, the feeds always have just the current show. If you want to get an old show (and because many Podcasting Clients aren't smart enough to not download the file more than once) you can always find them at http://www.hanselminutes.com.
  • I have, and will, also include the enclosures to this feed you're reading, so if you're already subscribed to ComputerZen and you're not interested in cluttering your life with another feed, you have the choice to get the 'cast as well.
  • If there's a topic you'd like to hear, perhaps one that is better spoken than presented on a blog, or a great tool you can't live without, contact me and I'll get it in the queue!

Enjoy. Who knows what'll happen in the next show?

Now playing: Matisyahu - Dispatch the Troops

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

XmlValidatingReader problems over derived XmlReaders

March 12, 2006 Comment on this post [3] Posted in Web Services | XmlSerializer | Bugs
Sponsored By

This whole validation ickiness deserved two posts, so I didn't mention it in the last XmlValidatingReader post.

The XML format that I'm parsing and validating isn't the savvyest of formats as it was created years ago before the XML Schema specification was complete. While it has a namespace and it's an official specification, the instance documents don't have a namespace. They are entirely "unqualified." So, basically I'm trying to validate XML documents with a namespace against a schema that expects namespaces.

Additionally, the elementFormDefault is set to "unqualified." There's a great explanation of what elementFormDefault means here.

The documents come in like this:

<FOO>
  <BAR>text</BAR>
</FOO>

Before I'd look hard at the schema I had assumed that I could load them with an XmlNamespaceUpgradeReader. This is a derivation of XmlTextReader that does nothing but lie about the Namespace of every element. I'm using System.Xml on .NET 1.1.

public class XmlNamespaceUpgradeReader : XmlTextReader

    {

        string oldNamespaceUri;

        string newNamespaceUri;

 

        public XmlNamespaceUpgradeReader( TextReader reader, string oldNamespaceUri, string newNamespaceURI ):base( reader )

        {

            this.oldNamespaceUri = oldNamespaceUri;

            this.newNamespaceUri = newNamespaceURI;

        }

 

        public override string NamespaceURI

        {

            get

            {

                // we are assuming XmlSchemaForm.Unqualified, therefore

                // we can't switch the NS here

                if ( this.NodeType != XmlNodeType.Attribute &&

                    base.NamespaceURI == oldNamespaceUri )

                {

                    return newNamespaceUri;

                }

                else

                {

                    return base.NamespaceURI;

                }

            }

        }

    }

For example, if I did this:

XmlTextReader reader = new XmlNamespaceUpgradeReader(
    File.OpenText("MyLameDocument.xml"),
    String.Empty,
    "http://thenamespaceiwant"); 


XmlDocument doc = new XmlDocument();

doc.Load(reader);

Console.WriteLine(doc.OuterXml);

I would end up with this resulting XML:

<FOO xmlns="http://thenamespaceiwant">
  <BAR xmlns="
http://thenamespaceiwant">text</BAR>
</FOO>

Seemed like this would validate. Well, not so much. The document, as you can see, is fine. It's exactly what you'd expect. But, the I remember/noticed that the document was elementFormDefault="unqualified" meaning that only the root node needs the namespace. So...

public class XmlRootNamespaceUpgradeReader : XmlTextReader

{

    string oldNamespaceUri;

    string newNamespaceUri;

 

    public XmlRootNamespaceUpgradeReader( TextReader reader, string oldNamespaceUri, string newNamespaceURI ):base( reader )

    {

        this.oldNamespaceUri = oldNamespaceUri;

        this.newNamespaceUri = newNamespaceURI;

    }

 

    public override string NamespaceURI

    {

        get

        {

            // we are assuming XmlSchemaForm.Unqualified, therefore

            // we can't switch the NS here

            if ( Depth == 0 && this.NodeType != XmlNodeType.Attribute &&

                    base.NamespaceURI == oldNamespaceUri )

            {

                return newNamespaceUri;

            }

            else

            {

            return base.NamespaceURI;

            }

        }

    }

 

    public override string Prefix

    {

        get

        {

            if(Depth == 0 && this.NodeType == XmlNodeType.Element)

            {

                return "x";

            }

            return null;

        }

    }

 

}

...which results in a document like this:

<x:FOO xmlns:x="http://thenamespaceiwant">
  <BAR
>text</BAR>
</x:FOO>

This document should now validate, and it fact it does in my test applications. When the document is loaded directly from a test file it works fine. When I run it directly through one of the extended "fake-out" XmlTextReaders, it doesn't work. It's as if my readers don't exist at all, even though their code does indeed execute.

To be clear:

Original Doc -> XmlTextReader -> XmlValidatingReader -> doesn't validate (as expected)
Original Doc -> XmlNamespaceUpgradingReader -> XmlValidatingReader -> doesn't validate (but it should!)
Original Doc -> XmlNamespaceUpgradingReader -> XmlDocument -> write to file -> read from file -> XmlValidatingReader -> doesn't validate (as expected, it's "overqualified")
Original Doc -> XmlRootNamespaceUpgradingReader -> XmlDocument -> write to file -> read from file -> XmlValidatingReader -> DOES VALIDATE (as expected)

Why don't the "fake-out" XmlTextReaders work when chained together and feeding the XmlValidatingReader directly, but they do work when there's an intermediate format?

A few things about the XmlValidatingReader in .NET 1.1 (since it's obsolete in 2.0). While its constructor takes the abstract class XmlReader, internally it insists on an XmlTextReader. This is documented, but buried IMHO. Reflector shows us:

XmlTextReader reader1 = reader as XmlTextReader;
if (reader1 == null)
{
    throw new ArgumentException(Res.GetString("Arg_ExpectingXmlTextReader"), "reader");
}

<conjecture>When a class takes an abstract base class - the one it "should" - but really requires a specific derivation/implementation internally, it's a good hint that the OO hierarchy wasn't completely thought out and/or a refactoring that was going to happen in a future version never happened.</conjecture>

Regardless, System.Xml in .NET 2.0 is much nicer and as well though-out as System.Xml 1.x was, 2.0 is considerably more thought out. However, I'm talking about 1.1.

<suspicion>I take this little design snafu as a strong hint that the XmlValidatingReader in .NET 1.1 has carnal knowledge of XmlTextReader and is probably making some assumptions about the underlying stream and doing some caching rather than taking my fake-out XmlReader's word for it.</suspicion> 

If you're on, or were on, the System.Xml team let me know what the deal is and I'll update this post.

I know that the XmlRootNamespaceUpgradingReader works because the XML is correct when it's written out to an intermediate. However, the InfoSet that the XmlValidatingReader acts on is somehow now the same.  How did we solve it? Since XmlValidatingReader needs an XmlTextReader that is more "legit," we'll give it one

Original Doc -> XmlRootNamespaceUpgradingReader -> XmlDocument -> CloneXmlReader -> XmlValidatingReader -> DOES VALIDATE

This is cheesy, but if a better way is found at least it's compartmentalized and I can fix it in one place. We quickly run through the input XmlTextReader, write the Infoset out to a MemoryStream and return a "fresh" XmlTextReader and darn it if it doesn't work just fine.

/// <summary>

/// Makes an in memory complete, fresh COPY of an XmlReader. This is needed

/// because the XmlValidatingReader takes only XmlTextReaders and isn't fooled

/// by our XmlNamespaceUpgradingReader.

/// </summary>

/// <param name="reader"></param>

/// <returns></returns>

protected XmlTextReader CloneReader(XmlTextReader reader)

{

    MemoryStream m = new MemoryStream();

    XmlTextWriter writer = new XmlTextWriter(m,Encoding.UTF8);

    while (reader.Read())

    {

        writer.WriteNode(reader,false);

    }

    writer.Flush();

    m.Seek(0,SeekOrigin.Begin);

    XmlTextReader returnedReader = new XmlTextReader(m);

    return returnedReader;

}

Madness. Many thanks to Tomas Restrepo for his help and graciousness while debugging this issue!

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook bluesky subscribe
About   Newsletter
Hosting By
Hosted on Linux using .NET in an Azure App Service

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.