Pages

Thursday, 18 June 2009

XPath

XPath came handy lately as I needed to look-up an in-memory XML document.
I know it's probably smoother with Linq to Xml but my project had to compile under VS2005 so I used the .NET 2.0 library which does not contain Linq but contains XPath.

Starting from the following xml file:

<?xml version="1.0" encoding="utf-8"?>
<pigs>
  <piggy infected="false" name="bob"/>
  <piggy infected="false" name="alfred"/>
  <piggy infected="true" name ="rodrigo">
    <disease name="swine flu"/>
    <disease name="boredom"/>
    <disease name="pig blues"/>
    <address>confidential</address>
    <phonenumber>01234546576</phonenumber>
  </piggy>
</pigs>


To load the XML in memory:

XmlDocument doc = new XmlDocument();

try

{

doc.Load("piggy.xml");

}

catch (XmlException e)

{

Console.WriteLine("Could not load the file. Detail: " + e.Message);

}



To query elements based on their name:

XmlNodeList allPigs = doc.SelectNodes("/pigs/piggy"); // Returns all nodes called 'piggy' located inside the root-level node called 'pigs'.

foreach (XmlNode node in allPigs)

Console.WriteLine(node.Name + " " + node.Attributes["name"].Value);



To query elements based on their attribute name:

// Returns only infected pigs

XmlNodeList infectedPigs = doc.SelectNodes("/pigs/piggy[@infected='true']");

foreach (XmlNode node in infectedPigs)

Console.WriteLine(node.Name + " " + node.Attributes["name"].Value);


To return a single node (same query as above but only one node is expected):


// Returns the single infected pig

XmlNode infectedPig = doc.SelectSingleNode("/pigs/piggy[@infected='true']");

if (infectedPig != null)

Console.WriteLine(infectedPig.Name + " " + infectedPig.Attributes["name"].Value);


To do a query relative to the current node:
All queries above were made relative to the top of the document. But it you call SelectNodes against an XmlNode, you can do a query relative to that node. Just ommit the '/':

// Query relative to the current node. Returns all diseases for the infected pig

XmlNodeList diseases = infectedPig.SelectNodes("disease");

foreach (XmlNode node in diseases)

Console.WriteLine(node.Name + " " + node.Attributes["name"].Value);



Resources:
MSDN: XPath Syntax
LINQ to XML queries
XML Support in SQL Server 2005

Monday, 1 June 2009

Webforms vs MVC (London .NET User Group)

100 people listened to Sebastien Lambla and Phil Wistanley at the Microsoft customer centre in Victoria. They talked for 2 hours without a break. Two hours is a long time but the formula they adopted was entertaining and fun. Seb was supporting MVC, Phil Webforms. They kept rotating at the mike, going through humorous slides, throwing jokes at each other and interacting with the audience. Seb:
  • Webforms has a tendency to hide HTML and actually generates a lot of goo.
  • MVC relies on you knowing HTML but once you learn it, things get pretty easy.
  • Webform's page lifecycle is complex
  • Webforms is for morons.
Phil:
  • MVC is too complicated,
  • Webforms has a lot of ready-made controls, lots of 3rd party vendors
  • Uses a familiar event model
  • MVC is for hippies.
To sum it up, Webforms is for building apps quickly (better suited for the financial world), MVC is good if you need a high level of quality and testability.