对带有换行符的字符串使用 string.Format 时出错

Error while using string.Format for strings with newline chars

使用 string.Format

时出现错误

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

代码:

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

分配 errorMessage 时错误出现在第一行。在分配的字符串末尾放置 {"method":"xpath","selector":"//div[@title='Apple nc.']"}"; 当你想在你的字符串中使用 " 时,你必须在前面加上一个 \,否则它被假定为字符串的结尾。

我的缺点:使用 string.Format 和插值。删除后有效 string.Format