Thursday, July 19, 2007 

content/binary/serialize-nvc.jpg

I had a NameValueCollection embedded inside a larger object. I needed to serialize the larger object into XML and back. Unfortunately, NameValueCollection is not XML serializable. Why I do not know.

A blog comment from Tim Erwin got me started in the right direction. Implement IXmlSerializable and do the work by hand in ReadXml and WriteXml.

Tim's implementation turned out to be overly simple. It didn't handle an empty collection well, nor did it leave the XmlReader in a good state.

I used SGen to examine the deserialization of a List<String> to figure out what else needed to be done.

The following ReadXml seems to work. If I expected to receive XML from untrusted sources, I would make this more robust.

 public void ReadXml(XmlReader reader)
{
if (reader.IsEmptyElement)
return;

while (reader.Read()
&& reader.NodeType != XmlNodeType.EndElement
&& reader.NodeType != XmlNodeType.None)
{
if (reader.NodeType == XmlNodeType.Element && reader.LocalName == "Header")
{
reader.MoveToAttribute("name");
string name = reader.Value;
reader.MoveToAttribute("value");
string value = reader.Value;
Add(name, value);
}
}
reader.ReadEndElement();
}

public void WriteXml(XmlWriter writer)
{
foreach (string name in nvc.Keys)
{
writer.WriteStartElement("Header");
string value = nvc[name];
writer.WriteAttributeString("name", name);
writer.WriteAttributeString("value", value);
writer.WriteEndElement();
}
}

public XmlSchema GetSchema( )
{
return null;
}

I also found that I needed to implement custom Equals and GetHashCode, as the NameValueCollection implementations didn't seem to do what I wanted.

 // Have to override GetHashCode() as two apparently identical NameValueCollections
 // will have different hash codes.
 public override int GetHashCode()
{
int hash = nvc.Count;

foreach (string name in nvc)
{
hash = 757 * hash + 101 * nvc[name].GetHashCode() + name.GetHashCode();
}

return hash;
}

public bool Equals(HeadersCollection that)
{
if (ReferenceEquals(that, null))
return false;

if (ReferenceEquals(this, that))
return true;

// Have to explicitly compare the contents of the collections // as NameValueCollection.Equals doesn't seem to do what we want. // Note: this is independent of order. if (nvc.Count != that.nvc.Count)
return false;

foreach (string name in nvc)
{
if (nvc[name] != that.nvc.Get(name))
return false;
}

return true;
}

public static bool Equals(HeadersCollection headersA, HeadersCollection headersB)
{
if (headersA == null)
return (headersB == null);

if (ReferenceEquals(headersA, headersB))
return true;

return headersA.Equals(headersB);
}

public override bool Equals(object obj)
{
if (obj is HeadersCollection)
return Equals((HeadersCollection) obj);

return false;
}
posted on Friday, July 20, 2007 6:31:59 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]
Sunday, July 15, 2007 

http://images.amazon.com/images/P/043935806X.01.MZZZZZZZ.jpg

Title: The Order of the Phoenix
Author: J.K. Rowling
Rating: 3.5 stars out of 5
Publisher: Scholastic
Copyright: 2003
ISBN: 043935806X
Pages: 870
Keywords: fantasy
Reading period: 14-15 July, 2007

Having just seen the new Harry Potter movie, I decided to reread this book and the Half-Blood Prince before the release of the final book, next weekend.

The movie omits vast swathes of plot, of course, but delivers a competent retelling of the book.

Voldemort came back to life at the end of the previous book, but only Harry Potter has seen him and few, apart from Dumbledore and the reconstituted Order of the Phoenix, believe him. A tinpot dictator from the Ministry of Magic, Dolores Umbridge, is sent to Hogwarts to clamp down on troublemakers. Harry goes through his fifth year, studying for the OWL examinations, fighting with Umbridge, and being increasingly troubled by dreams linking him to Voldemort.

The book is entertaining and moves the Harry Potter saga along, but it's too long. Rowling, like Stephen King, is too much of a publishing phenomenon for editors to have a strict hand with her.

Harry is in full-blown, awkward adolescence: sulky, misunderstood, clumsy around girls, and rebellious. He fights with all of his well-meaning friends, Ron, Hermione, and Sirius.

In the movie, particularly, I thought that Harry was ill-used by Dumbledore as a pawn in the struggle against Voldemort. The book allows for more nuance. A lot of grief and misunderstanding could have been avoided if only Dumbledore had been forthcoming much earlier. Of course, that would have ruined most of the plot.

posted on Monday, July 16, 2007 6:42:21 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]

http://images.amazon.com/images/P/0446693294.01.MZZZZZZZ.jpg

Title: The Accusers
Author: Lindsey Davis
Rating: 3.5 stars out of 5
Publisher: Mysterious Press
Copyright: 2003
ISBN: 0446693294
Pages: 369
Keywords: historical mystery
Reading period: 5-11 July, 2007

The Accusers is one of the more recent titles in Lindsey Davis's long-running series about Marcus Didius Falco, an informer (private detective) in ancient Rome. Davis's prose is slyly witty with an occasional leavening of snark.

Falco and Associates look into the death of a senator who committed suicide after being convicted of corruption. Was it really suicide? A complicated courtroom drama ensues.

On a par with other books in the series. Entertaining, amusing, and plenty of plot twists.

posted on Monday, July 16, 2007 6:41:41 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]
Wednesday, July 04, 2007 

http://images.amazon.com/images/P/0743470567.01.MZZZZZZZ.jpg

Title: Dead I Well May Be
Author: Adrian McKinty
Rating: 4 stars out of 5
Publisher: Pocket Books
Copyright: 2003
ISBN: 0743470567
Pages: 367
Keywords: crime
Reading period: 4 July, 2007

Michael Forsythe is an illegal immigrant from Northern Ireland, working for a crime boss in Harlem in 1992. When he sleeps with his boss's girlfriend, he and three others are set up to take the fall for a drug bust in Mexico. He breaks out of a hellhole prison, losing a foot and his friends along the way, and makes his way back to New York to exact revenge.

McKinty writes lush, atmospheric prose, with a good turn in dialog. Forsythe grows from a bright, feckless teenager, with a future ahead of him in crime, into a hardened, vengeful survivor.

posted on Thursday, July 05, 2007 1:40:18 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]
Tuesday, July 03, 2007 

http://images.amazon.com/images/P/0060882328.01.MZZZZZZZ.jpg

Title: Ally
Author: Karen Traviss
Rating: 4 stars out of 5
Publisher: Eos
Copyright: 2007
ISBN: 0060882328
Pages: 388
Keywords: SF
Reading period: 30 June-3 July, 2007

This is the sequel to Matriarch, one of the very first books I reviewed, back in December 2006.

As with its predecessor, this book does not admit of an easy summary and it too should be read in sequence.

The themes include alien contact, ecocide, genocide, the undesirable consequences of immortality, and the clash of personalities. The plot is character-driven and fast-paced, with multiple twists.

posted on Wednesday, July 04, 2007 3:17:17 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]

http://images.amazon.com/images/P/067131839X.01.MZZZZZZZ.jpg

Title: Pyramid Scheme
Author: Dave Freer, Eric Flint
Rating: 3.5 stars out of 5
Publisher: Baen
Copyright: 2001
ISBN: 067131839X
Pages: 418
Keywords: SF, humor
Reading period: 27-29 June, 2007

Pyramid Scheme is another humorous science fiction novel from the authors of Rats, Bats, and Vats and The Rats, The Bats, & The Ugly.

An alien probe, in the shape of a pyramid, lands in Chicago and starts growing rapidly. It captures some of the people in the vicinity and sends them into an alternate universe, where most of them die within hours. A handful survive and start to thrive. The new universe contains the Greek and Egyptian gods and characters from Greek mythology, including the ever-untrustworthy Odysseus.

The plot is too silly to explain further, but it's an enjoyable romp, as the core characters triumph over the odds.

posted on Wednesday, July 04, 2007 3:16:41 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]

http://images.amazon.com/images/P/0743457900.01.MZZZZZZZ.jpg

Title: Tinker, Tailor, Soldier, Spy
Author: John le Carré
Rating: 4.5 stars out of 5
Publisher: Scribner
Copyright: 1974
ISBN: 0743457900
Pages: 317
Keywords: spy, thriller
Reading period: 23-26 June, 2007

After panning Prior Bad Acts and Adept, I needed to read a good book. I found it in John le Carré's classic cold war spy novel, Tinker, Tailor, Soldier, Spy.

George Smiley, quiet, unassuming, pudgy, and easily overlooked, is recently retired from the Service (MI6, the British intelligence agency). He is secretly tasked with finding a mole in the highest reaches of the Service, run by Karla, a KGB spymaster. The mole can only be one of the four most senior men. Smiley begins piecing together the evidence from stolen files, interrogating former colleagues, and re-examining his own past.

This is not at all the typical spy novel, full of fast-paced car chases and shootouts. The book is subtle, cerebral, and character-driven, with little action. Smiley may not be capable of running across the street, but he can certainly run a sting operation.

Le Carré masterfully weaves a web of deceit and intrigue, which enmeshes the reader. He depicts a world of moral ambiguity, painted in shades of gray, where motives are murky.

Highly recommended.

posted on Wednesday, July 04, 2007 3:16:01 AM (Pacific Daylight Time, UTC-07:00) 
#    Comments [0]