Here is something simple. Hope it helps.
public bool SaveXrefState ( Database db, DBDictionary dict, string name )
{
Xrecord Xrec = new Xrecord();
ResultBuffer rb = new ResultBuffer();
using ( Transaction Trans = db.TransactionManager.StartTransaction() )
{
db.ResolveXrefs( false, true );
XrefGraph xrGph = db.GetHostDwgXrefGraph( false );
for ( int i = 1; i < xrGph.NumNodes; i++ )
{
XrefGraphNode xrNode = xrGph.GetXrefNode( i );
BlockTableRecord btr = Trans.GetObject( xrNode.BlockTableRecordId, OpenMode.ForRead ) as BlockTableRecord;
if ( btr == null ) continue;
rb.Add( new TypedValue( 5, btr.Handle ) );
rb.Add( new TypedValue( 62, ( btr.XrefStatus == XrefStatus.Unloaded ? 1 : 0 ) ) );
}
Xrec.Data = rb;
if ( !dict.IsWriteEnabled ) dict.UpgradeOpen();
dict.SetAt( name, Xrec );
dict.DowngradeOpen();
Trans.Commit();
}
return true;
}