Recently in XML in Visual Studio Category

When working with XPath be it in XSLT or C# or Javascript, apostrophes and quotes in string literals is the most annoying thing that drives people crazy. Classical example is selections like "foo[bar="Tom's BBQ"]. This one actually can be written correctly as source.selectNodes("foo[bar=\"Tom's BBQ\"]"), but what if your string is something crazy as A'B'C"D" ? XPath syntax doesn't allow such value to be used as a string literal altogether- it just can't be surrounded with neither apostrophes nor quotes. How do you eliminate such annoyances? 

The solution is simple: don't build XPath expressions concatenating strings. Use variables as you would do in any other language. Say no to

selectNodes("foo[bar=\"Tom's BBQ\"]") 
and say yes to
selectNodes("foo[bar=$var]")

How do you implement this in .NET? System.Xml.XPath namespace provides all functionality you need in XPathExpression/IXsltContextVariable classes, but using them directly is pretty much cumbersome and too geeky for the majority of developers who just love SelectNodes() method for its simplicity.

The Mvp.Xml project comes to rescue providing XPathCache class:

XPathCache.SelectSingleNode("//foo[bar=$var]",
    doc, new XPathVariable("var", "A'B'C\"D\""))

And this is not only stunningly simple, but safe - remember XPath injection attacks?

You can download latest Mvp.Xml v2.0 drop at our new project homepage at the Codeplex.

If you have tried to create or use code snippets in any of XML languages in Visual Studio 2005 you probably stumbled across the issue of using shortcuts. In C# or VB you just type code snippet's shortcut name and press Tab, it just works. In XML it doesn't :(. I've spent couple of hours reflecting into XML editor to see why it doesn't work (that was an interesting waste of time anyway) and when I just found out how it works I also found out (via Aaron Skonnard) it's documented at MSDN. RTFM, RTFM, RTFM.

Unfortunately inserting code snippets by shortcut name in XML editor of Visual Studio 2005 isn't as smooth as in C# or VB. Two more keystrokes: < and Esc. So press <, Esc, then shortcut and then Tab. That sucks, but I can live with it.

Hmm, according to Stan Kitsis there is at least 6 ways to associate an XML Schema with an XML document in Visual Studio 2005:

1. Schemas Property on your XML document
2. Inline inside your XML document
3. xsi:schemaLocation or xsi:noNamespaceSchemaLocation attributes in your XML document
4. Open Document Window.
5. Anywhere in your current Project
6. In the Schema Cache Directory or from a Schema Catalog file.
Is it actually sane? Now I can easily imagine a situation when I cannot be sure against which XML Schema file I'm validating my documents. Lots of flexibility, too much or just right? What is your take?

Can one change a value of a variable in XSLT? Unexpected answer - yes, when debugging XSLT in Visual Studio 2005 Beta2. I'm not sure even if it's a bug. Actually it can be quite useful when debugging to be able to change some values, right? Do other XSLT debuggers allow it?

I'm learning new XML stuff in Visual Studio 2005 Beta2. So far I'm still overwhelmed! New System.Xml is sooooo cool, elegant and professional, kudos to the WebData team, this is really a piece of programming art. Still I filed 4 bugs so far in just one hour.

Finally something interesting from the Channel 9: Ken Levy shows new cool XML tools in Visual Studio 2005 - improved XML editor, XML Intellisense, schemas, XML code snippets, XSLT editing and debugging etc etc etc. 41 min video, still worth watching.