When they read Arthurian legends in English class early in 11th grade, Xander's favorite character had been Sir Gawain. He was brave and strong and not afraid of a little gore. Plus, he was kind of like a sidekick, being merely one, fairly ordinary knight among the shining stars of the Round Table. He didn't let being a sidekick stop him from taking on the Green Knight, though, and that's why Xander liked him. Sir Gawain managed to be the sidekick and a hero.
It was nearly three years after they'd finished with Arthur and moved on to Chaucer when Xander realized he wasn't Gawain. He was sitting on Giles' couch with a book balanced on his knee, open to a mention of the Lady of Shallott, when a sudden recognition shot through him. He was definitely not Gawain. He was Lancelot. Oh, not in the 'raised by an enchantress and destined to be the greatest knight in the realm' kind of way. Though he would happily have traded that for his 'raised by unhappy drunks and destined to bowl well and bring the snacks' life any day.
The thing he remembered most about Lancelot was that Lancelot had been the trusted friend of the king who had lusted inappropriately after the king's consort. Of course, Buffy was the king, which was where the analogy got messy, but Xander had never paid that much attention to artistic devices, anyway. The point was, it was the inappropriate lusting he had in common with Lancelot.
In other words, Riley was hot and Xander was noticing.
He tried not to notice, but Riley didn't make it easy for him. He kept smiling this bright, open smile that Riley probably meant as, "Hey, nice to see you," but that Xander's body translated as, "You should have sex with me right now." The fact that his body responded more enthusiastically to message in those smiles than to Anya saying the same thing (out loud and in English) was telling.
After his epiphany, Xander started reading up on his Arthurian legends, sneaking Le Morte d'Arthur and modern Arthur novels out of his pocket or backpack when Willow wasn't around. If she caught him reading them, Xander knew she would want to discuss and explain and encourage, and he was so not up for a book club about this. As he read, he started to really feel bad for Arthur. He was pretty sure Arthur had loved Guinevere, but he could see why she might not have thought so. Arthur always had quests to attempt, knights to recruit, and witches and family members to fight. (And if Xander cast Faith as Morgan le Fay in his little mental Camelot, no one had to know that but him.) With Arthur so busy, so caught up in duty and adventure, it was easy to see how Guinevere could start to question her importance in his life. It was easy to see why she would take comfort in the attention and affection of a friend.
Despite all his deep, literary introspection and shallow, infatuated lusting, Xander didn't see any danger in spending time with Riley. The guy was deeply, gooshily in love with Buffy, after all, and Xander genuinely enjoyed spending time with him. Riley was more than just a bright smile and a cute ass; he was funny and smart and willing to do stupid guy things with Xander, like playing Frisbee golf in the park and watching the entire Die Hard trilogy in one sitting.
They didn't talk about emotions or relationships, so Xander wasn't expecting Riley to suddenly tell him that Buffy didn't love him. For a second, all he could do was blink stupidly and think, "God, Buffy, I thought you were smarter than Arthur." But Riley was hurting and Xander hated that, so instead of tracking Buffy down and ordering her to straighten out her priorities, Xander wrapped his arms around Riley and proved he wasn't any smarter than Lancelot, either.
Staring into Riley's eyes and feeling his big hands clutching at Xander's shirt, he knew this was a bad idea. The story was a tragedy, after all. Bad idea or not, Xander leaned forward and Riley surged to meet him, and Xander knew why Lancelot had done it. It wasn't about his own desires, wasn't about betraying his friend.
It was about making Guinevere feel loved.