public XmlAttribute InsertBefore( XmlAttribute newNode, XmlAttribute refNode ) {
if ( newNode == refNode )
return newNode;
if (refNode == null)
return Append(newNode);
if (refNode.OwnerElement != parent)
throw new ArgumentException(Res.GetString(Res.Xdom_AttrCol_Insert));
if (newNode.OwnerDocument != null && newNode.OwnerDocument != parent.OwnerDocument)
throw new ArgumentException(Res.GetString(Res.Xdom_NamedNode_Context));
if (newNode.OwnerElement != null)
Detach( newNode );
int offset = FindNodeOffset( refNode.LocalName, refNode.NamespaceURI );
Debug.Assert( offset != -1 ); // the if statement above guarantees that the ref node is in the collection
int dupoff = RemoveDuplicateAttribute( newNode );
if ( dupoff >= 0 && dupoff < offset )
offset--;
InsertNodeAt( offset, newNode );
return newNode;
}