Normally, XML is untyped, by itself it certainly is, although there are a plethora of schema languages in the wild, all too willing to project types onto an otherwise untyped structure. XML Schema and what have you. And so when C# 4.0 introduced the dynamic type, some people thought this may actually be the ticket out of syntax hell for dealing with XML in C#. Not so in my oppinion. An example
Now book will of type dynamic and will contain an XElement underneath. Method dispatch on this dynamic object will identify the method name being called and basically use that name to do _xelement.Element(methodName). There are problems with this approach.
First of all, XML name syntax is not a syntactic subset of the C# method identifier syntax, and so one cannot cleanly map XML names into C# method names. And when introducing namespaces all thoughts of compatibility goes out the door.
Let’s just take plain XSLT as an example
The colon is the first show-stopper. The next is the dash, which is the minus operator.
Some XML dialects also use a dot. XAML is one such language.
Oops, now we have the quintessential operator in C#, the member invocation operator, now being used by the XML and we have to express this in naming convention somehow.
The abstraction breaks down.
There is a deCast video on Channel 9 about this approach to deal with XML and while it’s clever, it just breaks down in my oppinion. I’d be very happy to be corrected in this matter because dynamic dispatch is really cool for this.
The video even goes as far as introducing magic names such as Parent and Nodes that are interpreted differently than element names. Parent will fetch the parent element, for example. This will collide if you have an XML name called Parent. I suppose in that example just don’t use Parent. The extensibility of XML allows new names to be introduced and used without breaking existing code – it’s one of its strengths – and so one might unexpectedly run into an XML name called Parent in the wild, in a format where one thought this was safe. So suddenly language evolution has broken ones code. Not good!
Nice idea though.
XML 1.0 name syntax
“:” | [A-Z] | “_” | [a-z] | [#xC0-#xD6] | [#xD8-#xF6] | [#xF8-#x2FF] | [#x370-#x37D] | [#x37F-#x1FFF] | [#x200C-#x200D] | [#x2070-#x218F] | [#x2C00-#x2FEF] | [#x3001-#xD7FF] | [#xF900-#xFDCF] | [#xFDF0-#xFFFD] | [#x10000-#xEFFFF]
NameStartChar | “-” | “.” | [0-9] | #xB7 | [#x0300-#x036F] | [#x203F-#x2040]
C# 1.0 identifer syntax
An identifier-or-keyword that is not a keyword
_ (the underscore character U+005F)
A Unicode character of classes Lu, Ll, Lt, Lm, Lo, or Nl
A unicode-escape-sequence representing a character of classes Lu, Ll, Lt, Lm, Lo, or Nl
A Unicode character of classes Mn or Mc
A unicode-escape-sequence representing a character of classes Mn or Mc
A Unicode character of the class Nd
A unicode-escape-sequence representing a character of the class Nd
A Unicode character of the class Pc
A unicode-escape-sequence representing a character of the class Pc
A Unicode character of the class Cf
A unicode-escape-sequence representing a character of the class Cf