需要使用数据合同序列化程序填充 XML 来生子级
本文关键字:XML 填充 程序 序列化 数据 合同 | 更新日期: 2023-09-27 18:30:13
我有一个方法可以将错误消息放在 1 xml 中并将其发送到客户端。如果出现错误,错误可以是几个,我返回XMLErrMessage中的错误列表。我想在评论中显示它们,但每个错误都显示为 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;
CollectionDataContract
属性允许您控制集合元素名称,但是由于它只能针对类或结构,因此您必须使用所需的协定创建自定义 List<T>
子类,如下所示:
[DataContract(Namespace = "")]
[KnownType(typeof(CommentList))]
public class CFCConnectResponse
{
[DataMember]
public int StatusCode;
[DataMember]
public string StatusDescription;
[DataMember(Name="comments")]
public CommentList Comments;
}
[CollectionDataContract(ItemName = "comment", Namespace="")]
public class CommentList : List<string>
{
public CommentList()
: base()
{
}
public CommentList(params string[] strings)
: base(strings)
{
}
public CommentList(IEnumerable<string> strings)
: base(strings)
{
}
}
然后,要测试:
public static class TestCFCConnectResponse
{
static CFCConnectResponse CreateTest()
{
return new CFCConnectResponse()
{
StatusCode = 101,
StatusDescription = "here is a description",
Comments = new CommentList("XMLErrMessage1", "XMLErrMessage2", "XMLErrMessage3"),
};
}
public static void Test()
{
var response = CreateTest();
try
{
var xml = DataContractSerializerHelper.GetXml(response);
Debug.Write(xml);
var newResponse = DataContractSerializerHelper.GetObject<CFCConnectResponse>(xml);
Debug.Assert(newResponse != null);
Debug.Assert(response.StatusCode == newResponse.StatusCode);
Debug.Assert(response.StatusDescription == newResponse.StatusDescription);
Debug.Assert(newResponse.Comments.SequenceEqual(response.Comments));
}
catch (Exception ex)
{
Debug.Assert(false, ex.ToString());
}
}
}
这将生成以下输出,没有断言:
<?xml version="1.0" encoding="utf-16"?>
<CFCConnectResponse xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<StatusCode>101</StatusCode>
<StatusDescription>here is a description</StatusDescription>
<comments>
<comment>XMLErrMessage1</comment>
<comment>XMLErrMessage2</comment>
<comment>XMLErrMessage3</comment>
</comments>
</CFCConnectResponse>
更新
如果更改CFCConnectResponse.CommentList
以获取和设置CommentList
需要对旧代码进行太多更改,则可以执行以下操作:
[DataContract(Namespace = "")]
[KnownType(typeof(CommentList))]
public class CFCConnectResponse
{
[DataMember]
public int StatusCode;
[DataMember]
public string StatusDescription;
[IgnoreDataMember]
public List<string> Comments { get; set; }
[DataMember(Name = "comments")]
private CommentList SerializableComments
{
get
{
return new CommentList(Comments);
}
set
{
Comments = value.ToList();
}
}
}
这将在序列化和反序列化CommentList
时保留List<string> Comments
属性。