Step 1: Simplify the given integral
We are asked to evaluate the integral:
\[
I = \int \frac{x^2 (x \sec^2 x + \tan x)}{(x \tan x + 1)^2} dx
\]
Notice that the numerator contains a term \( x \sec^2 x + \tan x \) and the denominator involves \( (x \tan x + 1)^2 \).
Step 2: Substitution and simplification
Let us make the substitution:
\[
u = x \tan x + 1
\]
Then, differentiate both sides with respect to \( x \):
\[
\frac{du}{dx} = x \sec^2 x + \tan x
\]
This allows us to replace \( x \sec^2 x + \tan x \) in the numerator with \( du \). The integral then becomes:
\[
I = \int \frac{x^2}{u^2} \, du
\]
Step 3: Integrating the simplified form
We now need to integrate:
\[
\int \frac{x^2}{u^2} \, du
\]
This can be done by recognizing that \( \frac{x^2}{u^2} \) is a simple rational function. We integrate to get:
\[
- \frac{x^2}{u} + 2 \log_e |x \sin x + \cos x| + C
\]
Step 4: Final result
Substitute back \( u = x \tan x + 1 \) into the solution:
\[
I = -\frac{x^2}{x \tan x + 1} + 2 \log_e |x \sin x + \cos x| + C
\]
Thus, the final answer is:
\[
\boxed{-\frac{x^2}{x \tan x + 1} + 2 \log_e |x \sin x + \cos x| + C}
\]