I think this might work for you:
from thetable as b
where b.scanned_id like 'A%'
and b.scanned_order between a.scanned_order+1 and a.next_student_order-1
for xml path('')
) as answer_code
from (select a.scanned_id
,min(isnull(b.scanned_order,99999999)) as next_student_order
from thetable as a
left outer join thetable as b
and b.scanned_id not like 'A%'
where a.scanned_id not like 'A%'
) as a
The above query has not been syntax checked, so please bear over with me, if this is the case.