deCast - Dynamic Xml with C# 4.0

Play deCast - Dynamic Xml with C# 4.0
Sign in to queue


The Dynamic capabilities of C# 4.0 provide the ability to communicate with dynamic languages such as IronRuby and IronPython, but C# developers can build dynamic objects, as well.  In this screencast, we will illustrate how you can take advantage of the dynamic functionality enabled in C# 4.0 to access Xml data in a more natural way.  We will illustrate how you can go from writing code like: ele.Attribute["lastName"].Value to just ele.lastName.



Download this episode

The Discussion

  • User profile image

    This is pretty nice but broken. There's too much magic going on. The XML GI syntax (and still more the ns'd GI syntax) does not match up with C# method name syntax. In particular C# method names are far more restricted than XML (namespace) names, they cannot express them. Now adding magic names such as Nodes, Parent, etc. will only compound the problem. What happens if a document actually happens to have an element called Parent or Nodes? So based on a quick review, this approach does not appear sound, as much as I love syntactic sugar.


    Now the idea to abstract over attribute and element names is quite sweet. I like the idea and this is also the approach that RDF/XML takes. It just needs to be an explicit abstraction taken by the client not built into the API as a default, thus projecting that assumption onto all existing XML.


    As a way to illustrate how dynamic is useful this is not so bad - except it, in my oppinion, teaches an unsound practice for dealing with XML; at least it is not general-purpose. One has to know a priori that such and such name collisions (Parent, Nodes?) will not occur (there goes extensibility) and that cannot express such and such names (XML name syntax, including names in popular XML dialects such as XSLT, where you can have: xslt:value-of).

  • User profile image

    Any chance you could post the sample implementation of this code please?

    Thanks - this look good.

  • User profile image

    The screencast goes over your fun little XML DSL but doesn't ACTUALLY talk about how to take advantage of any of C# 4.0's dynamic objects.
    Very neat, but now I have to search online for documentation on DynamicObject and methods like GetMember, assuming those are part of the CLR or DLR and not something you wrote.
    Could you please post the code from the episode so people can take a look at the actual code to figure out how to take advantage of these new features?

  • User profile image

    DynamicObject reference:
    A blog post that shows example implementations of TryInvokeMember, TryGetMember, TrySetMember, and more:

Add Your 2 Cents