Inline XML Linq manipulation sample

A quick example of inline xml and Linq

 

Code

[code:c#]

void LinqToXmlSample()
{
  Console.WriteLine("{0} : Start", new StackTrace(0, true).GetFrame(0).GetMethod().Name);

  XDocument xDocument = new XDocument(
    new XElement("people",
      new XElement("person",
        new XAttribute("sex", "male"),
        new XElement("FirstName", "Brian"),
        new XElement("LastName", "Keating")),
      new XElement("person",
        new XAttribute("type", "female"),
        new XElement("FirstName", "Dustin"),
        new XElement("LastName", "Turkey"))));

  IEnumerable<XElement> elements =
    xDocument.Element("people").Descendants("FirstName");

  /*  First, I will display the source elements.*/
  foreach (XElement element in elements)
  {
    Console.WriteLine("Source element: {0} : value = {1}",
      element.Name, element.Value);
  }

  /*  Now, I will display the ancestor elements for each source element.*/
  foreach (XElement element in elements.Ancestors())
  {
    Console.WriteLine("Ancestor element: {0}", element.Name);
  }

  Console.WriteLine("{0} : Finish", new StackTrace(0, true).GetFrame(0).GetMethod().Name);
}

[/code]

 

Output

LinqToXmlSample : Start
Source element: FirstName : value = Brian
Source element: FirstName : value = Dustin
Ancestor element: person
Ancestor element: people
Ancestor element: person
Ancestor element: people
LinqToXmlSample : Finish

 

[Update August 2012: Little did I know when i wrote this post just how much I would use Linq to xml, it saved me from a life of xslt Smile ]

Resource Cleanup and Lambda Expressions

A neat way of always cleaning up resources is to use Lambdas as data.

Take the following

Source

[code:c#]

    internal interface ITryCatchReport
    {
        void Try(Action<IServer> action);
    }

    internal class TryCatchReport : ITryCatchReport
    {
        public TryCatchReport(IServer server)
        {
            _server = server;
        }

        public void Try(Action<IServer> action)
        {
            try
            {
                action(_server);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e.Message);
                // Clean up resources
                // Report errors
            }
        }

        private IServer _server;    
    }

 

[/code]

 

Usage

[code:c#]

TryCatchReport safeInvoker = new TryCatchReport(_data.Server);
safeInvoker.Try(x =>
{
x.MakeInterfaceCall();
});

[/code]

We are now guaranteed that in the case of an exception that the resources will get cleaned up.

 

Usage with code blocks

If you wish to execute many statements in the action look at this sample.

[code:c#]

private List<WFActionDefinition> GetActionDefinitions()
{
    if (_actionDefinitions == null)
    {
        safeInvoker.Try(x =>
            {
                x.Do1();
                x.DoSomething();
                OtherFunc();
            });
    }

    return _actionDefinitions;
}

[/code]