Technical Fun

Archive for December 2007

I got an XML document that looks like this.

<AreaList>
<Area id=”1″>
<name>Brave Land</name>
<description>An area where the brave may live forever</description>
</Area>
</AreaList>

Now, if I want to search and fetch the text inside the “name” tag based on a supplied “id”, I can rely faithfully on LINQ.

XDocument doc = XDocument.Load(PATH_TO_XML_DOCUMENT);
var x = from area in doc.Descendants(“Area”)
          where (int)area.Attribute(“id”) == MY_SUPPLIED_ID
          select (string)area.Element(“name”);

and consume it using x.First()

aaah …. how readable, how lovely, Nirvana!!!