Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/281.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从xml文件检索时,新行字符不保留。我使用了cdata_C#_Xml_Mstest - Fatal编程技术网

C# 从xml文件检索时,新行字符不保留。我使用了cdata

C# 从xml文件检索时,新行字符不保留。我使用了cdata,c#,xml,mstest,C#,Xml,Mstest,我不熟悉使用mstest框架编写单元测试用例,我一直在从xml文件中检索新行字符作为预期值的输入。下面是一个测试方法 public void ExtractNewLineTest() { MathLibraray target = new MathLibraray(); // TODO: Initialize to an appropriate value string expected = TestContext.DataRow["ExpectedValue"].ToStri

我不熟悉使用mstest框架编写单元测试用例,我一直在从xml文件中检索新行字符作为预期值的输入。下面是一个测试方法

public void ExtractNewLineTest()

{
    MathLibraray target = new MathLibraray(); // TODO: Initialize to an appropriate value
    string expected = TestContext.DataRow["ExpectedValue"].ToString(); => I am retrieving the value from the xml file.
    string actual;
    actual = target.ExtractNewLine();
    Assert.AreEqual(expected, actual);
}
下面是xml内容

<ExtractNewLineTest>
      <ExpectedValue><![CDATA[select distinct\tCDBREGNO,\r\n\tMOLWEIGHT,\r\n\tMDLNUMBER\r\nfrom Mol]]></ExpectedValue>
 </ExtractNewLineTest>
如果我们看到上面的值,将为\n和\r添加额外的斜杠。
请让我知道,我如何能断言这个值。谢谢

您的XML文件不包含换行符。它包含一个反斜杠,后跟一个
t
r
n
。从XML文件中忠实地读取该组合,然后调试器在向您显示该组合时跳过反斜杠

如果您想对从XML读取的值应用“C#字符串转义”规则,可以这样做,但您应该知道确实需要这样做。一种过于简单的方法是:

expectedValue = expectedValue.Replace("\\t", "\t")
                             .Replace("\\n", "\n")
                             .Replace("\\r", "\r")
                             .Replace("\\\\", "\\");

在某些情况下,这样做是错误的——例如,如果你有一个真正的反斜杠,后跟一个
t
。不过对你来说已经足够了。如果没有,您必须编写一个“正确”的替换,从一开始就读取字符串并处理各种情况。

您能否帮助我在这种情况下如何断言。那太好了@vasujc:一旦你得到了正确的期望值,你就可以正常断言了。非常感谢你的输入!!!它确实帮助我解决了我的问题。我也会跟踪
expectedValue = expectedValue.Replace("\\t", "\t")
                             .Replace("\\n", "\n")
                             .Replace("\\r", "\r")
                             .Replace("\\\\", "\\");