Search code examples
c#string.format

Error while using string.Format for strings with newline chars


Getting error while using string.Format

errorMessage is "OpenQA.Selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"xpath","selector":"//div[@title='Client nc.']"}"

code:

errorMessage = (exceptions.CurrentDotNetExceptionMessage.ToString() + "\n" + exceptions.CurrentDotNetInnerException.ToString().Split(new[] { '\r', '\n' }).FirstOrDefault()) ?? string.Empty;


 defectDetails = string.Format($"Issue: {actualResult} \n\n" +
                $"Steps To Reproduce: \n {stepsToReproduce} \n\n" +
                $"Failed Step: \n {failedStep} \n" +
                $"Failed Element: \n {failedElement} \n" +
                $"Test Data: \n {testData} \n\n" +
                $"Actual Result: \n {actualResult} \n" +
                $"Expected Result: \n {expectedResult} \n" +
                $"Error Message: \n {errorMessage.ToString()} \n\n" +
                $"Browser: {browser} \n" +
                $"Enviornment: {environment}\n\n" +
                $"Stack Trace: {stackTrace}" +
                $"\n---------------------------------------------------------------------------------------------------\n\n"
                );``` 
Error Line:   
```string.Format($"Error Message: \n {errorMessage} \n");```

Error: string.FormatException

Solution

  • My bad: Used string.Format along with interpolation. Worked when removed string.Format