private XmlNode GetColumnInsertAfterLocation(DataRow row, DataColumn col, XmlBoundElement rowElement)
{
XmlNode prev = null;
XmlNode node = null;
// text only columns appear first
if (IsTextOnly(col))
return null;
// insert location must be after free text
for (node = rowElement.FirstChild; node != null; prev = node, node = node.NextSibling)
{
if (!IsTextLikeNode(node))
break;
}
for (; node != null; prev = node, node = node.NextSibling)
{
// insert location must be before any non-element nodes
if (node.NodeType != XmlNodeType.Element)
break;
XmlElement e = node as XmlElement;
// insert location must be before any non-mapped elements or separate regions
if (_mapper.GetRowFromElement(e) != null)
break;
object schema = _mapper.GetColumnSchemaForNode(rowElement, node);
if (schema == null || !(schema is DataColumn))
break;
// insert location must be before any columns logically after this column
if (((DataColumn)schema).Ordinal > col.Ordinal)
break;
}
return prev;
}