I would create a class that represents the record (with all of the columns as properties, and provide an additional property that looks something like this):
public XElement AsXElement
{
get
{
XElement value = new XElement("BBid",
new XElement("trnid", this.TrnID.ToString()),
new XElement("stnid", this.StnID.ToString()),
...
new XElement("PIGGYBACK", (this.PiggyBack == null)?"", this.PiggyBack.ToString())
);
return value;
}
}
I do it this way all the time.
There may be a better way...
I also have some extension functions that allow me to set the properties in the object from XML and they're used to handle null values and provide exception handling during the setting process.
Using Extension Methods To Avoid XML Problems[
^]