I have a method that put the error messages in 1 xml and send it to client. In case of error, errors can be several, I am returning the list pf errors that are in XMLErrMessage. I want to show them in comment but each error as 1 xml child:
<comments>
<comment>XMLErrMessage1</comment>
<comment>XMLErrMessage2</comment>
<comment>XMLErrMessage3</comment>
</comments>
this is my method:
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;
}
and this is contract class:
public class CFCConnectResponse
{
[DataMember]
public int StatusCode;
[DataMember]
public string StatusDescription;
[DataMember]
public List<string> Comments;
The
CollectionDataContract
attribute allows you to control the collection element names, however since it can only target a class or struct, you must create a custom subclass ofList<T>
with the desired contract, like so:And then, to test:
This produces the following output, with no asserts:
Update
If changing
CFCConnectResponse.CommentList
to get & set aCommentList
requires too many changes to legacy code, you can do the following:This preserves the
List<string> Comments
property while serializing & deserializing aCommentList
.