Welcome to Manicprogrammer Sign in | Join | Help

Linq to SQL usage of XML

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!
}

Published Friday, March 27, 2009 11:44 AM by willeke
Filed under: , , ,

Comments

No Comments
Anonymous comments are disabled