The molecular recognition process of the carbohydrate-binding module family 36 (CBM36) was examined theoretically. The mechanism of xylan binding by CBM36 and the role of Ca2+ were investigated by the combined use of molecular dynamics simulations and the 3D reference interaction site model method. The CBM36 showed affinity for xylan after Ca2+ binding, but not after Mg2+ binding. Free-energy component analysis of the xylan-binding process revealed that the major factor for xylan-binding affinity is the electrostatic interaction between the Ca2+ and the hydroxyl oxygens of xylan. The van der Waals interaction between the hydrophobic side chain of CBM36 and the glucopyranose ring of xylan also contributes to the stabilization of the xylan-binding state. Dehydration on the formation of the complex has the opposite effect on these interactions. The affinity of CBM36 for xylan results from a balance of the interactions between the binding ion and solvents, hydrophilic residues around xylan, and the hydroxyl oxygens of xylan. When CBM binds Ca2+, these interactions are well balanced; in contrast, when CBM binds Mg2+, the dehydration penalty is excessively large.