Entries:
Comments:
Posts:

Loading User Information from Channel 9

Something went wrong getting user information from Channel 9

Latest Achievement:

Loading User Information from MSDN

Something went wrong getting user information from MSDN

Visual Studio Achievements

Latest Achievement:

Loading Visual Studio Achievements

Something went wrong getting the Visual Studio Achievements

deCast - Dynamic Xml with C# 4.0

Download

Right click “Save as…”

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.

Tags:

Follow the Discussion

  • Bent Rasmussenexoteric stuck in a loop, for a while

    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).

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

    Thanks - this look good.

  • remiremi

    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?

  • remiremi

    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

Remove this comment

Remove this thread

close

Comments Closed

Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation, please create a new thread in our Forums,
or Contact Us and let us know.