我有一个处理XML的wcf,并将响应(作为XML)返回给客户端。 这是返回响应的方法。
public string ProcessXML(CommonLibrary.Model.TransferData dto, bool Authenticated)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(ResponseBack));
MemoryStream ms = new MemoryStream();
utility.utilities utl = new utility.utilities();
string XMLErrMessage = string.Empty;
if (Authenticated)
{
if (!string.IsNullOrEmpty(dto.xml))
{
XMLErrMessage = utl.validateXML(dto.xml, xsdFilePath, currentSchema);
if (string.IsNullOrEmpty(XMLErrMessage))
{
dcs.WriteObject(ms, new ResponseBack() { StatusCode = 101, StatusDescription = "Success" });
ms.Position = 0;
}
else
{
dcs.WriteObject(ms, new ResponseBack() { StatusCode = 201, StatusDescription = "XML Validation Fails" });
ms.Position = 0;
}
}
}
else
{
dcs.WriteObject(ms, new ResponseBack() { StatusCode = 401, StatusDescription = "Authentication Fails" });
ms.Position = 0;
}
string s = new StreamReader(ms).ReadToEnd(); // xml result
return s;
}
我正在测试这种方法,我得到3 xmls taht正在返回s。
XML#1:
<ResponseBack xmlns=\"http://schemas.datacontract.org/2004/07/CommonLibrary.Model\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><StatusCode>401</StatusCode><StatusDescription>
Authentication Fails</StatusDescription</ResponseBack>
XML#2:
<ResponseBack xmlns=\"http://schemas.datacontract.org/2004/07/CommonLibrary.Model\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><StatusCode>101</StatusCode><StatusDescription>Success</StatusDescription></ResponseBack>
XML#3:
<ResponseBack xmlns="http://schemas.datacontract.org/2004/07/CommonLibrary.Model" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><StatusCode>201</StatusCode><StatusDescription>XML Validation Fails</StatusDescription></ResponseBack>
如果你看一下XML#1和#2,在xmls前面有一个额外的\,我怎么能摆脱它: xmlns = \“http://schemas.datac
溶液: 我在visual studio中以调试模式查看结果,因此它显示了额外/当我在文本框中打印出结果时它没问题。
答案 0 :(得分:2)
我100%确定DataContractSerializer
不会产生那些反斜杠。您要么在Visual Studio的监视中查看您的值,它会根据C#语法转义字符串,或者程序的某些其他部分会更改XML。