Navigating XML (LINQ to XML series – part 4)

In my last few posts on LINQ to XML (part 1, part 2 and part 3) I’ve shown you a starter on navigating around XML data. In this post I’ll continue to show you how to navigate through XML data by showing you how to navigate around sibling elements.

First consider this code:

XElement root = new XElement("root",
    new XElement("FirstChild"),
    new XElement("SecondChild"),
    new XElement("ThirdChild"),
    new XElement("FouthChild"),
    new XElement("FifthChild"));

Which produces the following XML structure:

<root>

<FirstChild />

<SecondChild />

<ThirdChild />

<FouthChild />

<FifthChild />

</root>

We can access the ThirdChild with this code:

XElement child = root.Element("ThirdChild");

From that point, we can also get access to its siblings.

To access the siblings that occur before the element we have a reference to then we can use ElementsBeforeSelf. As with Elements this returns an IEnumerable<XElement> object which allows us to iterate over the result, like this:

IEnumerable<XElement> elements = child.ElementsBeforeSelf();

foreach (XElement element in elements)
    Console.WriteLine(element);

The result is:

<FirstChild />

<SecondChild />

Conversely, we can get the siblings that come after the element we have a reference to with ElementsAfterSelf. Like this:

IEnumerable<XElement> elements = child.ElementsAfterSelf();

The result in this case will be:

<FouthChild />

<FifthChild />

Technorati Tags: ,,,

Postgraduate .NET Application Development

Glasgow Caledonian University is currently developing a new Postgraduate course on .NET Application Development. This course will combine the expertise of their lecturers with the industry-standard Microsoft Official Curriculum. This course will offer a series of academic qualifications, starting with a Postgraduate Certificate and potentially leading on to a Postgraduate Diploma and an MSc, and will also provide preparation for Microsoft Certification in .NET specialist areas. A Postgraduate Certificate is likely to take 3 months of full-time study or 6 months part-time. A full MSc takes 1 calendar year of full-time study and 2 to 3 years part-time.

The university has a short survey that is designed to find out how they can best deliver this course to meet the needs of potential students. They are interested in the views of potential students, employers who wish to develop staff and employers who recruit developers. If you think the course might be of interest to you, please take a moment to answer these questions.

The survey can be found here

LINQ to XML event summary

The LINQ to XML talk was earlier today. We had a slightly smaller turn out than expected and I’m wondering if it was the weather (it was warm), people went on holiday (it is June – but then why sign up?) or if it was the string quartet in the lobby that people shied away from.

Yes, you read that right! Tonight our event was accompanied by a string quartet playing Pachelbel, Bach and pieces from many other composers. But it wasn’t for our event, one of the neighbouring events in the university had the entertainment laid on. And if you’ve never heard a string quartet playing up close and live before then boy are these guys LOUD!

Anyway, for those that turned up we had a fun night and we issued prizes of a copy of JetBrains Resharper and a copy of Daniel Moth‘s famous book (something to do with Mobile Development).

If you want to learn more about LINQ to XML then you might want to read my series of posts on the subject, it is still in it early stages at the moment but more posts will added shortly.

The presentation was a new style for me. I had but two slides in total. An introduction slide and an overview of LINQ slide. For the remainder of the two hours I staying in Visual Studio. I’m hoping that was more effective that sitting in slides the whole time, or bouncing back and forwards between slides and code.

Scottish Developers May 2008 Newsletter


Welcome

It seems a long time since I last put out a newsletter. In fact it was before Developer Day Scotland which seems to me to be long since past. Now that DDS is over for this year I am concentrating on getting the evening events organised in Glasgow for the remainder of the year. This is an ideal time to email me and let me know what subjects you would like to know about and I’ll try and find speakers that can talk on that topic.

But Scottish Developers is about serving software developers across Scotland and with that in mind the exciting news that we are amalgamating with the North East Scotland .NET User Group (NESDNUG) means that are going to be able to bring events to Dundee and hopefully further afield.

There are some fantastic events coming up in Scotland over the next couple of months. Between Scottish Developers and VBUG we will be taking a functional view of the world starting in Dundee when Barry Carr asks “What is Functional Programming?”, to get us one step closer to understanding Oliver Sturm is in Glasgow to show us “Functional Programming in C# 3.0” and one week later moves to Livingston to show us “F#” which is a functional programming language that targets the .NET framework.

As always, we are on the look out for new speakers. If you would like the opportunity to do a presentation on a software development topic from 10 minutes to 90 minutes then get in touch with me at colin@scottishdevelopers.com

Regards,
Colin Mackay on Behalf of Scottish Developers

Events

2-June-2008 @ 18:30 in Glasgow (BCS)
BCS Glasgow Branch AGM 2008 with Survey of Job Market
FREE

The speaker will be Sam Wasom, Principal Consultant, Edinburgh Office of Computer Futures. Sam will talk about the “ICT job market Scotland 2008 – Opportunities and Skills requirements”. There will be as before, an opportunity for those interested to have an individual meeting with the Computer Future’s people.

4-June-2008 @ 18:30 in Edinburgh (SQL Server UG)
Using Visual Studio Team Edition for DB Professionals
FREE – Registration required

Microsoft Visual Studio Team Edition for Database Professionals for the first time makes the database developer a first class citizen in the Microsoft development family. In this presentation Richard Fennel will cover the complete development life cycle using this tool, addressing issues such as schema management, source control, testing, data generation and deployment.

10-June-2008 @ 18:30 in Glasgow (Scottish Developers)
LINQ to XML: Everything but the kitchen SINQ
FREE – Registration optional

Colin Angus Mackay will be giving an introduction to the new XML classes in .NET 3.5, how they work and what can be done with them. After the break he’ll then show how the new XML classes can be used in LINQ (Language INtegrated Query) in order to get data out in the way that you want.

11-June-2008 @ 19:00 in Dundee (Scottish Developers / NESDNUG)
What is Functional Programming?
FREE – Registration optional

The past few years or so has seen a resurgence of interest in functional programming. In addition, some functional language features are finding there way into imperative languages. Why is this? There are couple of reasons; firstly, functional languages are well suited to certain problems. Secondly, functional languages have features that can help the developer to take advantage of the current and future generations of multi-core computers. This presentation aims to provide you with insights into what Functional programming is and show you how functional languages can help you develop systems for multi-core machines. This presentation will use Scala (http://scala-lang.org) to demonstrate the concepts that will be introduced.

8-July-2008 @ 18:30 in Glasgow (Scottish Developers)
Functional Programming in C# 3.0
FREE – Registration optional

The newest version of C# introduces a number of language features that finally make it very easy to employ a functional style of programming. However, from the perspective of an imperative programmer, there are lots of questions surrounding functional programming. Why would I want to do it at all? Should I drop all state information in my apps? What useful functional patters are applicable to C#? In this session Oliver Sturm uses many practical examples (and some theory) to try and answer these questions.

16-July-2008 @ 18:00 in Livingston (VBUG)
F# with Oliver Sturm
FREE – Registration Required

Taking efficiency one step further – F# Microsoft Research describes F# as “a scripted/functional/imperative/object-oriented programming language”. Combining all those aspects in one language is certainly not an easy task, but they’ve done a good job of it. F# is interesting both as a language to actually consider for your projects and as a source of features that might make it into the mainstream .NET languages tomorrow. The session uses many examples to give you a good general overview of F#. To complement the introductory session, Oliver is going to show some more advanced samples from his talk “Data Handling in F#” and there will also be room for Q&A as well as discussion.

Further Afield

September:
  SQL Bits III (Herts)
  Paul Cowan talks about ALT.NET and using Open Source technology (Glasgow)

November:
  VBUG Conference (Reading)
  TechEd Europe (Barcelona)
  Developer! Developer! Developer! (Reading)

Useful Links

MSDN Roadshow: This year the MSDN Roadshow was held at the Odeon Cinema in Braehead, Glasgow. This links to the slide decks and other resources from the event.

Developer Day Scotland: Slide decks, code samples and other resources are now available from the Developer Day Scotland website.

Feedback on Developer Day Scotland

I was recently one of the organisers for Developer Day Scotland (aka DDD Scotland) in Glasgow. It was on on the 10th of May and was the first of its kind in Scotland. I’ve also just finished putting together the event feedback for the various speakers and for the event itself. It is always interesting as a speaker when you receive your own feedback, and for me it has been interesting putting together the feedback for all the speakers to see what people liked and didn’t like.

If you weren’t at Developer Day Scotland and don’t know what it is I’ll explain it briefly. We had a conference in the centre of Glasgow based on the DDD events that are normally held at the Microsoft Campus at TVP in Reading. It was aimed at Developers mainly, but we also had a SQL track for DBAs. In total we had 132 delegates arrive on the day.

On the whole the comments were very positive. There is some room for improvement and we will be looking to improve in certain areas for next year. But there were some disappointing comments that suggested people either didn’t quite know what the event was actually trying to achieve or completely had the wrong end of the stick.

First, just to blow our own trumpet, some of the positive comments:

  • Good selection of sessions
  • Lot’s of T-shirts
  • It was free
  • It was on a Saturday (no time off work)
  • Well organised
  • Based in Glasgow

A small number of people commented on the location. Most comments were extremely positive because there have been very few (if any) events like this in Scotland, however, a couple were disappointed because it was so far from London. I have to say that from the point of view of people in Scotland the fact that the vast majority of conferences we see are located in the south east of England (in or within about an hour-ish of London) was actually one of the main drivers to bring the DDD format to Scotland. We had seen an increasing number of Scots make the trek south for each DDD and their only real complaint with it was the distance they had to travel. The DDD events will continue to be held in Reading for the foreseeable future and indeed DDD7 is going to be on November 22nd, but there are now regional DDD events (such as DDD Ireland and Developer Day Scotland) that will take place from time-to-time.

Some people were disappointed by the length of the sessions. They thought more time was needed as some speakers seemed to be compressing too much information into too short at time. I have to agree that in many cases the talk did need more time. There are a number of possible solutions to this. Speakers could indicate a length of time for their topic and we have variable length sessions. Obviously the logistics in this is more complicated, but it is doable. Another possible solution is to request all speakers prepare a longer talk. For some this will be good as their talk was a compressed version of a longer talk, but for others this may leave them with a large period of time at the end with nothing to do. We will work on this problem and try to produce something that allow speakers to run for the time they need to discuss their topic.

Another common bit of feedback was that it wasn’t so easy to tell what level each of the talks were at. Next year we will introduce a system where it will be easy to see what the prerequisites for a talk are.

One person wrote simply “not enough user interactions”. I’m not entirely sure what they mean by that, however, the experimental open spaces session we did at lunch time was very successful and we packed the room out, so we will aim to do more of these next year. Perhaps that will solve that particular problem as they tend to allow a lot of interaction between all the participants.

We had some comments about the fact that “all the sessions were based on Microsoft technology”. We did have some non-Microsoft based talks submitted, however the talks that appear on the day are voted in by the community. However, two of the talks we had on the day were in fact technology independent. We try and keep the process as democratic as possible. Hopefully next year we will get more non-MS topics submitted and more non-MS delegates voting on these sessions so we can have a more diverse programme of sessions available on the day.

We did receive some excellent feedback, but there is always room for improvement and I hope next year’s event will be even better. In the mean time, Scottish Developers will still be running events in the evenings.

Finally, I really must thank all those that helped out in the preparation and on the day. We had 15 excellent speakers without whom we would not have had an event at all. We had a number of people that helped put the day together including John Thomson, Martin Bell, Frank Kerrigan, Craig Murphy and Brian Hainey. And a number of people who helped out on the day including Duncan Lundie, Ged Mead, Beverley Hatchard and Catriona Mackay. Hopefully I’ve not missed anyone out.

Navigating XML (LINQ to XML series – Part 3)

In my last two posts (part 1 and part 2) I’ve been introducing you to the the new XML classes in .NET 3.5. In this post I’ll continue that and show you some of the ways to navigate through XML.

First of all, lets start with a simple hierarchy of XML elements:

XElement root = new XElement("FirstGeneration",
                    new XElement("SecondGeneration",
                        new XElement("ThirdGeneration",
                            new XElement("FourthGeneration"))));

Which looks like this when rendered as XML:

<FirstGeneration>

<SecondGeneration>

<ThirdGeneration>

<FourthGeneration />

</ThirdGeneration>

</SecondGeneration>

</FirstGeneration>

Also in the last post I used Element to get a specific element from the current element. For example:

XElement child = root.Element("SecondGeneration");

Elements

If root (or FirstGeneration) only had one child element called “SecondGeneration” then everything is fine, you get what you asked for. However, if it contains multiple children all called “SecondGeneration” then you will only get the first element called “SecondGeneration”.

For example, if you add the following to the code above:

root.Add(new XElement("SecondGeneration","2"));
root.Add(new XElement("SecondGeneration","3"));

You will get a piece of XML that looks like this:

<FirstGeneration>

<SecondGeneration>

<ThirdGeneration>

<FourthGeneration />

</ThirdGeneration>

</SecondGeneration>

<SecondGeneration>2</SecondGeneration>

<SecondGeneration>3</SecondGeneration>

</FirstGeneration>

If you want to get all those additional children called “SecondGeneration” you will need to use the Elements (note the plural) method. For example:

IEnumerable<XElement> children = root.Elements("SecondGeneration");

You’ll also note that we don’t get a collection returned but an enumerable. This give us the opportunity to exploit many of the new extension methods. But I’ll leave them for another post. For the moment, we just need to know that it make it easy for us to enumerate over the data using a foreach loop. For example:

foreach (XElement child in children)
{
    Console.WriteLine(child);
    Console.WriteLine(new string('-', 50));
}

This will write out:

<SecondGeneration>

<ThirdGeneration>

<FourthGeneration />

</ThirdGeneration>

</SecondGeneration>

————————————————–

<SecondGeneration>2</SecondGeneration>

————————————————–

<SecondGeneration>3</SecondGeneration>

————————————————–

Parent

Using the same root object as above, we can see how to navigate back up the XML tree using Parent.

XElement grandchild = root.Element("SecondGeneration").Element("ThirdGeneration");
Console.WriteLine(grandchild.Parent);

The result of the code will be that the SecondGeneration element is printed.

 

Technorati Tags: ,,,

Developer Day Scotland – The scores on the doors

Developer Day Scotland was last weekend and went very well. I’ve just finished collating all the speaker feedback and I’ve sent each speaker their feedback information (all anonymised). We had excellent speakers and I want to recognise the best speakers that we had on the day.

Was the session as expected?

  • Third place: Ben Hall
  • Second place: Barry Carr
  • First Place: Daniel Moth

Rate the presenter: Overall

  • Third place: Allan Mitchell
  • Second place: Guy Smith-Ferrier
  • First place: Daniel Moth

Rate the presenter: Slides

  • Third place: Richard Fennell
  • Second place: Oliver Sturm
  • First place: Daniel Moth

Rate the presenter: Demos

  • Third place: Guy Smith-Ferrier
  • Second place: Barry Carr
  • First place: Daniel Moth

Rate the presenter: Information

  • Third place: Oliver Sturm
  • Second Place: Guy Smith-Ferrier
  • First Place: Daniel Moth

Where's My Data? An introduction to Spatial Queries in SQL Server 2008

The slide deck used for my presentations to the

There is also a set of demo code to go along with the slide deck variants.

Further information

The following blog posts may also be useful: