5. The solution set of the equation $x^{2}+x-1=x e^{x^{2}-1}+\left(x^{2}-1\right) e^{x}$ is $A$ (where, $e$ is an irrational number, $e=2.71828$ $\cdots$). Then the sum of the squares of all elements in $A$ is ( ).
(A) 0
(B) 1
(C) 2
(D) 4
5. (C)

Let $y=x^{2}-1$. The original equation becomes
$$
x+y=x e^{y}+y e^{x} \text {, }
$$

which is $x\left(e^{y}-1\right)+y\left(e^{x}-1\right)=0$.
Since $x>0$ implies $e^{x}-1>0$, i.e., $x$ and $e^{x}-1$ always have the same sign, $y$ and $e^{y}-1$ also always have the same sign.

Therefore, $x\left(e^{y}-1\right) y\left(e^{x}-1\right)>0 \Leftrightarrow x^{2} y^{2}>0$. That is, when the two terms on the left side of (*) are not 0, these two terms have the same sign.
$$
\begin{array}{l}
\left.\therefore x\left(e^{y}-1\right)+y\left(e^{x}-1\right)=1\right) \Leftrightarrow\left\{\begin{array}{l}
x\left(e^{y}-1\right)=0, \\
y\left(e^{x}-1\right)=0
\end{array}\right. \\
\Leftrightarrow\left\{\begin{array} { l } 
{ x = 0 , y \in R , } \\
{ y = 0 , x \in R }
\end{array} \Leftrightarrow \left\{\begin{array}{l}
x=0, \\
y=0 .
\end{array}\right.\right.
\end{array}
$$

Also, $y=0$ means $x^{2}-1=0$, giving $x= \pm 1$.
Therefore, the original equation has roots $0,1,-1$, i.e., $A=\{0,-1$, $1\}$. The sum of the squares of its elements is 2.