Rediscovering the Obvious

…stumbling in the footsteps of greatness

Linq to SQL usage of XML

without comments

Just a reminder of something that has bitten us more than once. If you need to copy an XML value from one data object to another, use this syntax:

if( copiedPlacement != null )
{
  currentPlacement.FormatXML =
new XElement(copiedPlacement.FormatXML); // USUALLY CORRECT!
}

If you use the following, you’re actually doing a by-reference copy of the XElement, and changes to one will impact the other. Very subtle source of bugs if you’re not paying attention, and a real pain to find later.

if( copiedPlacement != null )
{
  currentPlacement.FormatXML =
copiedPlacement.FormatXML; // USUALLY WRONG!
}

Written by erwilleke

March 27th, 2009 at 11:44 am

Posted in Uncategorized

Leave a Reply