I called up Tim Ewald today to ask him a question I'm sure lots of people have run into before: “So, I've got an XPathNavigator, and I want to validate it against a schema. What API should I use?” He laughed, and then went on to explain how much the answer sucks. Basically, the only way to do it using the System.Xml APIs is to use the OuterXml property of the navigator to turn the XML back into a string, then suck it up into an XmlTextReader, then validate using that. This is a huge waste - it means you have to take something you already parsed, unparse it, then turn around and parse it again. Lame!
I knew I couldn't be the only one that had run into this problem before. Acting on a hunch, I ran a Google search on XPathNavigatorReader. Sure enough, I found this, which led me to this. In brief, it is the exact adapter layer I wanted, allowing me to get an XmlReader over any XPathNavigator. What's great is that the package was released just two days ago, so my timing was fortuitous.
There's a bunch of other cool stuff in there, too - definitely check it out if you're working with the System.Xml APIs at all..some of this stuff isn't even in Whidbey yet.
Some of the XML coolness in .Net is a bit convoluted. Especially if your trying to avoid too much overhead.
ReplyDeleteAs for the post in general, you used 'fortuitous' in a sentence, and in a web journal, craig++.
We try to maintain the highest standards of journalistic quality here at CraigBlog. ;)
ReplyDeleteFunny - Aaron Skonnard is on the list of devs on that sourceforge project. Looks like you know who to call next time ;-)
ReplyDeleteI noticed that. In fact, I was trying to get hold of him yesterday to ask him a question about it.
ReplyDeleteHi Craig, thanks for linking to the Mvp.Xml project. It's basically about suplementing what's missing in System.Xml. Most XML MVPs collaborate in the group.
ReplyDeleteAaron joined the group lately, although in particular the XPathNavigatorReader is not based on his previous work.
The project is pretty active, releasing a version every couple weeks, on average. The list of utilities on the project is here: http://weblogs.asp.net/cazzu/archive/2004/05/15/132532.aspx.
You may be interested in another common scenario solved by our project: how to avoid loading XmlDocuments just to return well-formed XML from a WebService. http://weblogs.asp.net/cazzu/archive/2004/05/31/144922.aspx. You can do it directly from an XPathNavigator or any XmlReader. :D
Very cool. I'm definitely going to be making use of this in several projects - having worked with System.Xml fairly extensively, I'm well aware of some of the shortcomings. Thanks for pulling this together!
ReplyDelete