The chemical reactions and pathways involving a xenobiotic compound, a compound foreign to living organisms.