XML Namespaces, XML Schemas, and .NET

While updating Spring.NET today, I found something odd about how .NET handles XML Namespaces.

Lets say you have the following XML:

<?xml version=“1.0” encoding=“utf-8” ?>
<foo>
<bar id=“id1”>
<fubar>“id1 fubar1”</fubar>
</bar>
<bar id=“id2”>
<fubar>“id1 fubar1”</fubar>
<fubar>“id2 fubar2”</fubar>
</bar>
</foo>

Now, let’s say that I wanted to process all of the <bar> elements. You can read this pretty easily from .NET using the following:

XmlDocument doc = new XmlDocument();
doc.Load(“NamespaceSpike.xml”);

XmlNodeList nodes = doc.SelectNodes(“//bar”);
foreach ( XmlNode barNode in nodes )
{
Console.WriteLine(barNode.OuterXml);
}

Pretty simple huh? The xpath statement “//bar” simply selects all of the bar elements within the document. Now, what happens if you add a namespace to this document, as such:

<?xml version=“1.0” encoding=“utf-8” ?>
<ns:foo xmlns:ns=“http://www.tempuri.org”>
<ns:bar id=“id1”>
<ns:fubar>“id1 fubar1”</ns:fubar>
</ns:bar>
<ns:bar id=“id2”>
<ns:fubar>“id1 fubar1”</ns:fubar>
<ns:fubar>“id2 fubar2”</ns:fubar>
</ns:bar>
</ns:foo>

Now, if you try to run the C# code found above on the new xml file, you will notice that nothing appears. See how I added the “xmlns “ attribute to the top of the root element? That’s called a namespace. I am assigning all the elements prefixed with “ns:” to the “http://www.tempuri.org” namespace. This is similar to namespaces in .NET, in that is allows XML authors to avoid conflicts when creating their elements.

Now, that’s all well and good, but how do we work with this? Easy, simply change the C# code to this:

XmlDocument doc = new XmlDocument();
doc.Load(“NamespaceSpike.xml”);
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
mgr.AddNamespace(“ns”, http://www.tempuri.org);
XmlNodeList nodes = doc.SelectNodes(“//ns:bar”, mgr);
foreach ( XmlNode barNode in nodes )
{
Console.WriteLine(barNode.OuterXml);
}

Notice that now we are creating an instance of the XmlNamespaceManager class, and passing it in when we call SelectNodes. This class simply resolves prefixes and namespaces when encountered in a XML file. Also, notice how I updated the the xpath statement to include the prefix we are searching for.

Easy huh? Now, here comes the hard part. This took me about 1 & 1/2 hours to figure out.

What do we do about default namespaces? For example, the following XML file is logically the same as the XML file above with the namespace prefixes, but this time, the namespace is set to the default of the document. Which means that a prefix doesn’t need to be supplied, as it is ‘assumed’:

<?xml version=“1.0” encoding=“utf-8” ?>
<foo xmlns=“http://www.tempuri.org”>
<bar id=“id1”>
<fubar>“id1 fubar1”</fubar>
</bar>
<bar id=“id2”>
<fubar>“id1 fubar1”</fubar>
<fubar>“id2 fubar2”</fubar>
</bar>
</foo>

Again, try to run the C# code against this xml file, and you will see that it works fine. How did that work out? You still have a reference to the “ns:” prefix from the previous XML file. So what’s going on?

Well, it turns out that xpath doesn’t understand default namespaces. So, for documents with default namespaces, you still need call AddNamespace() for the XmlNamespaceManager, but instead of passing in an empty string, as you would think, you simply have to pass in a random string. Then, just use the same random string in your xpath expression. That way, the xml file doesn’t need to change.

It’s strange, because I would have thought the default namespace would have a prefix of “”. But, it turns out that it can be anything, as long as it’s consistent when being referenced.

Falling in love with Java…..

….well to write programs for my Tivo.

Tivo went ahead an release the Tivo Home Media Engine SDK recently. It allows you to write small java programs to serve up to any series 2 Tivo on your network. I have just played around with the samples, and all I can say is ‘wow’. Some cool applications will come out from the OSS crowd, I can tell.

I, for one, can’t wait.

Second Tivo: Update

My plans for my second tivo did not go as planned.

Let’s review what I wanted to accomplish with my second tivo:

  1. No subscription. Record only on time & station.
  2. Watch content on any Tivo from any Tivo
  3. Stream Music & Photos to the second Tivo location

That’s not a bad little list. Let’s see how things worked out:

  1. No subscription. Record only on time & station. Nope. It turns out that Tivo disables ANY recording functionality without a valid subscription, which costs $299 lifetime or $13.99 a month. Needless to say, this sucks. AND you can’t download program updates without a subscription, either. So, I grab my credit card to pay for my monthly subscription. A nice little surprise is that since I have 1 tivo, my second tivo’s monthly subscription is only $6.95 a month, even though the other tivo is a lifetime subscription. So that’s OK, but not great and at least I get full functionality from both tivos.
  2. Watch content on any Tivo from any Tivo. This actually works as advertised. Sort of. What actually happens is when your tivo is on the same nerwork as another tivo, is that the other tivo actually appears at the bottom of the ‘Now Playing’ screen. From there you can browse the content of the other tivo BUT you can’t actually stream the content over the network. First you have to transfer the content from one tivo to the other. This takes a LONG time. Almost the length of the actual show. You can start watching the content at any time, but it may stop and studder while you are watching it. Also, this happens one program at a time. If you want to transfer more than one program at a time, each program goes into the transfer queue, and gets transferred one at a time.
  3. Stream Music & Photos to the second Tivo location. This works as expected, and I have no qualms about the functionality.

So, all in all, not a bad purchase. I still came out ahead for the 80 hr tivo I paid for. An I figure that at $6.95 a month, it would take about 3 1/2 years to pay for the lifetime subscription. By that time, I will have moved on to my next tivo ( or whatever PVR company is around by then ).

Ciao,

Griffin

A "must-have" .NET book for Java developers?

Ted Neward seems to think so.

He writes about MS’s new title Customizing MS’s .NET CLR here.

He supports the claim that Java developers need to read this book two ways:

Why should you care if you’re a Java guy? Because these kinds of hooks are necessary if Java is to keep up, number one, but also because these kinds of hooks would allow for commoditization of the JVM itself, creating a market for graduate students and entrepreneurs to create customized memory management algorithms, for example, that right now require those same entrepreneurs to create an entire JVM. The open-source world would have a field day, creating all sorts of vertical plug-ins at the JVM level that we could pick-and-choose, selecting whichever ones happen to fit our needs best–including a very-real, very-credible memory allocator that just keeps allocating until we run out of room in the heap (for short one-shot gotta-run-as-fast-as-frickin-possible batch jobs, for example).

I’ll admit that I have already run through the book at the store, and it blew me away. It detailed so much new stuff, a lot of it new for the .NET 2.0 release. It also went through explaining some things that I tried to figure out about the 1.x releases, but failed because of lack of docs.

Anyway, it’s a great book for .NET developers. I wouldn’t say it’s a must have for standard .NET developers, but let’s just say that if you enjoy Don Box & his Essential .NET, this book will find a home on your bookshelf.

One of the coolest freaking movies

A new Philip K. Dick movie is coming out, A Scanner Darkly.

You HAVE to see the trailer here.

I looks like the animated parts of Waking Life, but instead of just randomly overlaying it on top of real footage, the whole movie is “filmed” in this style.

Very, very cool….

5 hours of my life I won't get back.

I spent 5 hours yesterday trying to get MonoDevelop to run on my powerbook.

Hell, even getting it to compile would have been huge.

But because it’s being written from the linux users POV, it’s very difficult to get up and running on Mac OS X. I ended up in dependency hell. Follow this chain:

  • MonoDevelop requires GtkSourceView
  • GtkSourceView requires gtk+
  • gtk+ requires glib & atk
  • gtk+ also requires pango
  • pango requires fontbook
  • fontbook requires freetype

And that’s where I stopped and through my hands up in the air. After trying to get all of these to not only compile, but install correctly ( which means pkg-config and it’s wonderful environment variable PKG_CONFIG_PATH ), I just gave up after freetype said it required even more.

All this for GtkSourceView, which isn’t even the only dependency for MonoDevelop.

Geez….

Hunter S. Thompson: R.I.P. ( 1937 – 2005 )

I was shocked and saddened today to hear of Hunter S. Thompson’s death from a self-inflicted gun shot wound to the head.

Pound for pound, one of the greatest counter culture icons of our time, his works include “Hell’s Angels”, “Hey Rube”, and of course, “Fear and Loathing in Las Vegas”.

Two movies were made from his works:

  • The 1980 film “Where the Buffalo Roam”, where HST was played by Bill Murray.
  • The 1998 film “Fear and Loathing in Last Vegas”, where HST was played by Johnny Depp.

The latter is one of my favorite movies of all time. I think this quote from Fear and Loathing sums up HST’s life:

“There he goes. One of God’s own prototypes. Some kind of high powered mutant never even considered for mass production. Too weird to live, and too rare to die. “