【发布时间】:2014-11-12 18:43:00
【问题描述】:
我有一种方法可以将错误消息放入 1 个 xml 并将其发送给客户端。如果出现错误,错误可能是多个,我将返回 XMLErrMessage 中的 pf 错误列表。我想在评论中显示它们,但每个错误都显示为 1 个 xml 子项:
<comments>
<comment>XMLErrMessage1</comment>
<comment>XMLErrMessage2</comment>
<comment>XMLErrMessage3</comment>
</comments>
这是我的方法:
public string ProcessXML(CommonLibrary.Model.TransferData dto, bool Authenticated)
{
DataContractSerializer dcs = new DataContractSerializer(typeof(CFCConnectResponse));
MemoryStream ms = new MemoryStream();
utility.utilities utl = new utility.utilities();
List<string> XMLErrMessage =null;
if (Authenticated)
{
if (!string.IsNullOrEmpty(dto.xml))
{
XMLErrMessage = utl.validateXML(dto.xml, xsdFilePath, currentSchema);
if (XMLErrMessage.Count==1)
{
dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 101, StatusDescription = "Success" });
ms.Position = 0;
}
else
{
dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 201, StatusDescription = "XML Validation Fails", Comments=XMLErrMessage });
ms.Position = 0;
}
}
}
else
{
dcs.WriteObject(ms, new CFCConnectResponse() { StatusCode = 401, StatusDescription = "Authentication Fails" });
// ms.Position = 0;
}
string s = new StreamReader(ms).ReadToEnd(); // xml result
Console.WriteLine(s);
return s;
}
这是合同类:
public class CFCConnectResponse
{
[DataMember]
public int StatusCode;
[DataMember]
public string StatusDescription;
[DataMember]
public List<string> Comments;
【问题讨论】:
-
您是否阅读过任何关于此的 MSDN 文档...? MSDN DataContractSerializer
标签: c# xml wcf datacontractserializer