Representative John McGuire, a Republican, held off a challenge from former Representative Tom Perriello, a Democrat.