【发布时间】:2017-02-17 21:45:19
【问题描述】:
我们有一个包含以下合同的 WCF 设置:
[ServiceContract(
Namespace = Constants.Namespaces.HL7Namespace,
Name = Constants.Roles.ContentRequiredDocumentManagementSystem)]
// XmlSerializerFormat is needed to expose the HL7 schema fields without the "Field" suffix on each one, eg: idField
[XmlSerializerFormat]
public interface ICDARequest
{
[OperationContract(
// wsdl request action
Action = Constants.Namespaces.HL7Namespace + ":" + Constants.Interactions.RCMR_IN000029UV01 + "." + Constants.VersionType.NormativeCode + Constants.Version.InteractionVersion,
// wsdl operation name
Name = Constants.Interactions.RCMR_IN000029UV01,
// wsdl response action
ReplyAction = Constants.Namespaces.HL7Namespace + ":" + Constants.Interactions.RCMR_IN000030UV01 + "." + Constants.VersionType.NormativeCode + Constants.Version.InteractionVersion)]
SearchMessagesResponse SearchMessages(SearchMessagesRequest RCMR_IN000029UV01);
[MessageContract(
IsWrapped = false]
public class SearchMessagesResponse
{
[MessageBodyMember(
Name = State.Constants.Interactions.RCMR_IN000030UV01,
Namespace = State.Constants.Namespaces.HL7Namespace)]
public RCMR_IN000030UV01 data;
}
}
- 这些基于使用
xsd.exe的HL7v3 架构生成的类。 - 然后我们更改了架构以添加自定义元素,使用自定义命名空间来区分它并重新生成类。
- 这很好。
它补充说:
[System.Xml.Serialization.XmlTypeAttribute(TypeName = "BCCDX.DistributionStatus", Namespace = "urn:bccdx.ca")]
public partial class BCCDXDistributionStatus
{
[System.Xml.Serialization.XmlElementAttribute("receivedTime", Namespace = "urn:bccdx.ca", IsNullable = false)]
public TS receivedTime{...}
}
这是我们想要的。
然后在 WCF 服务中我们就可以使用新的类和成员了:
var distStatus = new BCCDXDistributionStatus();
distStatus.receivedTime = CreateTS(locStat.MessageDownloadDate);
然后这会被序列化并通过网络发送出去,如下所示:
<distributionStatus xmlns="urn:bccdx.ca">
<receivedTime value="201702150956-0800"/>
</distributionStatus>
这几乎是正确的。问题在于 XML 文档没有引用 "urn:bccdx.ca" 命名空间。我假设它会在序列化时自动添加到文档根元素中,但我错了。这就是最终的样子:
<RCMR_IN000030UV01 ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3">
...
</RCMR_IN000030UV01>
当真正想要的是:
<RCMR_IN000030UV01 ITSVersion="XML_1.0" xmlns="urn:hl7-org:v3" xmlns:x="urn:bccdx.ca">
...
</RCMR_IN000030UV01>
注意带有前缀的 urn:bccdx.ca
我想知道,如果我们可以添加多个命名空间,如何通过合同为生成的序列化消息 XML 添加前缀?我在网上看到了覆盖默认序列化程序的提示,但我宁愿不这样做。以前肯定有过这样的想法和处理过吗?
【问题讨论】:
-
1) 有什么方法可以将其扩展为minimal reproducible example,或者至少可以编译,而不会丢失类型? 2) 你说你使用
xsd.exe来生成你的类,你在某处申请[XmlSerializerFormat]吗?我没有在问题中看到它。 -
谢谢,是的,我编辑将
XmlSerializerFormat添加到原始问题中。
标签: c# xml wcf serialization messagecontract