deCast - Dynamic Xml with C# 4.0

Sign in to queue

Description

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.

Embed

Download

Download this episode

The Discussion

  • User profile image
    exoteric

    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
    philcockfie​ld

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

    Thanks - this look good.

  • User profile image
    remi

    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
    remi

    DynamicObject reference: http://msdn.microsoft.com/en-us/library/system.dynamic.dynamicobject.aspx
     
    A blog post that shows example implementations of TryInvokeMember, TryGetMember, TrySetMember, and more: http://weblogs.asp.net/dixin/archive/2010/06/08/a-todynamic-extension-method-for-fluent-reflection.aspx

Add Your 2 Cents