navigation
 Thursday, December 13, 2007

I recently installed the RTM version of Visual Studio 2008 (previously known as Orcas), which comes with C# 3.0 and .NET Framework 3.5. One of the coolest new things is LINQ, more on that in my next blog. The next coolest thing is the new C# 3.0 language support for lambda expressions, see the node  => part below:

 

XmlHelper.Visit(_rootTOCItem.ParentNode, "tocitem", 
  node => _tocItems.Add((XmlElement)node));

Where _tocItems is a List<XmlElement>  stored in the class that is calling this method.

This works because Visit accepts a delegate, and I am passing in an anonymous delegate above using the new syntax. Visit is just something I wrote that iterates over the nodes in an Xml tree. I could have used Link to Xml for that part, but this is a tree of XmlElements returned by an XmlDataProvider, and Linq to Xml operates on XElements.

 

public class XmlHelper
{
  public delegate void NodeVisitor(XmlNode node);
  
  public static void Visit( XmlNode node, string nodeNameFilter, NodeVisitor visitor )
  {
    if ( node.Name==nodeNameFilter)
      (node);
    if (node.HasChildNodes)
      foreach (XmlNode child in node.ChildNodes)
        (child, nodeNameFilter, visitor);
  }
}
posted on December 13, 2007  #    by John Waters  Comments [0] Trackback