IanG on Tap

Ian Griffiths in Weblog Form (RSS 2.0)

Blog Navigation

April (2018)

(1 item)

August (2014)

(1 item)

July (2014)

(5 items)

April (2014)

(1 item)

March (2014)

(1 item)

January (2014)

(2 items)

November (2013)

(2 items)

July (2013)

(4 items)

April (2013)

(1 item)

February (2013)

(6 items)

September (2011)

(2 items)

November (2010)

(4 items)

September (2010)

(1 item)

August (2010)

(4 items)

July (2010)

(2 items)

September (2009)

(1 item)

June (2009)

(1 item)

April (2009)

(1 item)

November (2008)

(1 item)

October (2008)

(1 item)

September (2008)

(1 item)

July (2008)

(1 item)

June (2008)

(1 item)

May (2008)

(2 items)

April (2008)

(2 items)

March (2008)

(5 items)

January (2008)

(3 items)

December (2007)

(1 item)

November (2007)

(1 item)

October (2007)

(1 item)

September (2007)

(3 items)

August (2007)

(1 item)

July (2007)

(1 item)

June (2007)

(2 items)

May (2007)

(8 items)

April (2007)

(2 items)

March (2007)

(7 items)

February (2007)

(2 items)

January (2007)

(2 items)

November (2006)

(1 item)

October (2006)

(2 items)

September (2006)

(1 item)

June (2006)

(2 items)

May (2006)

(4 items)

April (2006)

(1 item)

March (2006)

(5 items)

January (2006)

(1 item)

December (2005)

(3 items)

November (2005)

(2 items)

October (2005)

(2 items)

September (2005)

(8 items)

August (2005)

(7 items)

June (2005)

(3 items)

May (2005)

(7 items)

April (2005)

(6 items)

March (2005)

(1 item)

February (2005)

(2 items)

January (2005)

(5 items)

December (2004)

(5 items)

November (2004)

(7 items)

October (2004)

(3 items)

September (2004)

(7 items)

August (2004)

(16 items)

July (2004)

(10 items)

June (2004)

(27 items)

May (2004)

(15 items)

April (2004)

(15 items)

March (2004)

(13 items)

February (2004)

(16 items)

January (2004)

(15 items)

Blog Home

RSS 2.0

Writing

Programming C# 5.0

Programming WPF

Other Sites

Interact Software

API Service Evolution Again

Thursday 1 April, 2004, 02:22 PM

Matthew Adams emailed me to point out a useful technique that I didn't mention in my recent blog on service API evolution.

He noted that my somewhat contrived extensible schema makes life difficult for itself by putting the extensibility point right in the middle of the document. This is the place where it's most likely to trip up insufficiently robust clients. If you put it at the end, you are much less likely to catch out unwary clients. It should only break clients which actively check that you don't append any elements they weren't expecting.

Unfortunately, that's an easier mistake to make than you might think. Consider this code:

XmlReader rdr = GetXmlReaderFromSomewhere();
rdr.ReadStartElement("stuff");
string foo = rdr.ReadElementString("foo");
string bar = rdr.ReadElementString("bar");
rdr.ReadEndElement();   // Danger!

This expects to see documents of the form:

<stuff>
  <foo>Hello</foo>
  <bar>World</bar>
</stuff>

Unfortunately, it will fail even if you add new elements after the bar. The problem is that final line of code where it calls ReadEndElement. The semantics of that call are essentially 'I expect the very next content item in the document to be the end of the stuff element, and I want you to throw an exception if that's not the case'. So if you give it this instance:

<stuff>
  <foo>Hello</foo>
  <bar>World</bar>
  <unexpected>The Spanish Inquisition</unexpected>
</stuff>

the call to ReadEndElement will throw an XmlException complaining that it wasn't expecting to see another element at this point in the document. It's not all that easy to make it robust either - calling Skip doesn't work, because the cursor is not in the right place after the call to ReadElementString.

This is why I prefer to use either XPath, or XmlSerializer - both are much more robust in the face of these kinds of changes. XmlReader is a fairly low level API. It's efficient, being a streaming API, but it's not easy to make it work with changing schemas. The higher level XPath and XmlSerializer APIs are definitely the way to go if you want to be well placed to cope with change.

ObDreadfulPun: I was going to call this entry 'Tails (sic) of the Unexpected' but I'm not sure if that program was broadcast outside of the UK...

Copyright © 2002-2024, Interact Software Ltd. Content by Ian Griffiths. Please direct all Web site inquiries to webmaster@interact-sw.co.uk