Shortest path algorithm where the path can travel through at most 2 vertices in X ⊂ V
You never say what the goal is. I suspect that you want to find the shortest a path from a given vertex $s$ to a given vertex $t$ that passes through at most two vertices in $X$.
In this case you can assume w.l.o.g., that $s,t \not\in X$. Moreover, you can assume that the input graph is directed. If this is not the case, then  you can preliminarily replace each undirected edge $\{u,v\}$ with the two directed edges $(u,v)$ and $(v,u)$.
Let $G=(V,E)$ be the input graph and let $F = \{ (u,v) \mid u \in X\}$ be the set of outgoing edges from some vertex in $X$. Make a new graph $H$ containing three copies $G_1, G_2, G_3$ of the graph $(V, E \setminus F)$.
Then, augment the graph as follows:

For each edge $(u,v) \in F$ add:

an edge (of weight $0$) between the copy of $u$ in $G_1$ and the copy of $v$ in $G_2$.
an edge (of weight $0$) between the copy of $u$ in $G_2$ and the copy of $v$ in $G_3$.


Add a new vertex $t^*$ and the three edges (of weight 0) between each copy of $t$ in $G_1$, $G_2$, and $G_3$ and $t^*$.

You can now find a shortest path $P$ between the copy of $s$ in $G_1$ and $t^*$.
The list of vertices traversed by $P$ (except for the final vertex $t^*$) will induce the sought shortest path on $G$.