XML 反序列化为不同的 class?
XML deserialization into different class?
我的目标是使用 XML 数据传输松散地连接两个应用程序。
我可以轻松地以 XML 格式序列化和反序列化。 但是我可以在App1
中从class序列化并在App2
中在不同的class(与原始结构相同的结构)中反序列化吗?
C# 或 VB,无所谓。 VB中的结构示例:
App1:
Namespace Transmitter
<DataContract>
Public Class DataOut
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
应用程序 2:
Namespace Receiver
<DataContract>
Public Class DataIn ' structure actually matches Transmitter.DataOut from App1
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
在 App1
中,我可以将 Transmitter.DataOut
的实例序列化为 XML,但是如何将 App2
中生成的 XML 读取到实例中Receiver.DataIn
?我需要在 App2
中实施 Transmitter.DataOut
吗?或者可以解决这个(我同意很多次有用的)功能吗? 我不想因为共享相同的 class 名称而受到限制。
我对操作方法 ("is it viable?") 感兴趣,不一定在源代码中。如果需要,我可以 post 我的来源,但它是非常标准的来源,使用 DataContractSerializer
.
是的,你可以做到。所有必须匹配的是数据契约,而不是底层的 .Net 类型。来自 Data Contract Equivalence:
For a client to successfully send data of a certain type to a service, or a service to successfully send data to a client, the sent type does not necessarily have to exist on the receiving end. The only requirement is that the data contracts of both types be equivalent. (Sometimes, strict equivalence is not required, as discussed in Data Contract Versioning.)
For data contracts to be equivalent, they must have the same namespace and name. Additionally, each data member on one side must have an equivalent data member on the other side.
For data members to be equivalent, they must have the same name. Additionally, they must represent the same type of data; that is, their data contracts must be equivalent.
基于契约的序列化程序的基本优势之一是它不要求底层类型相同。这允许实现独立于有线格式,并且 enables communication between completely different architectures -- including for example Java and .NET.
您需要将 data contract namespace and name 显式设置为在两个系统中相同,例如:
Namespace Transmitter
<DataContract(Name:= "Data", [Namespace]:="http://www.MyNameSpace.com")> _
Public Class DataOut
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
或者,您可以使用 ContractNamespaceAttribute
:
为整个程序集设置命名空间
<Assembly:ContractNamespaceAttribute("http://www.MyNameSpace.com", ClrNamespace:="Transmitter")>
有关更多信息,请参阅 Data Contract Names。
另请注意,DataContractSerializer
是 order sensitive,因此合同中成员的顺序必须匹配。
我的目标是使用 XML 数据传输松散地连接两个应用程序。
我可以轻松地以 XML 格式序列化和反序列化。 但是我可以在App1
中从class序列化并在App2
中在不同的class(与原始结构相同的结构)中反序列化吗?
C# 或 VB,无所谓。 VB中的结构示例:
App1:
Namespace Transmitter
<DataContract>
Public Class DataOut
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
应用程序 2:
Namespace Receiver
<DataContract>
Public Class DataIn ' structure actually matches Transmitter.DataOut from App1
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
在 App1
中,我可以将 Transmitter.DataOut
的实例序列化为 XML,但是如何将 App2
中生成的 XML 读取到实例中Receiver.DataIn
?我需要在 App2
中实施 Transmitter.DataOut
吗?或者可以解决这个(我同意很多次有用的)功能吗? 我不想因为共享相同的 class 名称而受到限制。
我对操作方法 ("is it viable?") 感兴趣,不一定在源代码中。如果需要,我可以 post 我的来源,但它是非常标准的来源,使用 DataContractSerializer
.
是的,你可以做到。所有必须匹配的是数据契约,而不是底层的 .Net 类型。来自 Data Contract Equivalence:
For a client to successfully send data of a certain type to a service, or a service to successfully send data to a client, the sent type does not necessarily have to exist on the receiving end. The only requirement is that the data contracts of both types be equivalent. (Sometimes, strict equivalence is not required, as discussed in Data Contract Versioning.)
For data contracts to be equivalent, they must have the same namespace and name. Additionally, each data member on one side must have an equivalent data member on the other side.
For data members to be equivalent, they must have the same name. Additionally, they must represent the same type of data; that is, their data contracts must be equivalent.
基于契约的序列化程序的基本优势之一是它不要求底层类型相同。这允许实现独立于有线格式,并且 enables communication between completely different architectures -- including for example Java and .NET.
您需要将 data contract namespace and name 显式设置为在两个系统中相同,例如:
Namespace Transmitter
<DataContract(Name:= "Data", [Namespace]:="http://www.MyNameSpace.com")> _
Public Class DataOut
<DataMember>
Public Header As String
<DataMember>
Public Content As String
End Class
End Namespace
或者,您可以使用 ContractNamespaceAttribute
:
<Assembly:ContractNamespaceAttribute("http://www.MyNameSpace.com", ClrNamespace:="Transmitter")>
有关更多信息,请参阅 Data Contract Names。
另请注意,DataContractSerializer
是 order sensitive,因此合同中成员的顺序必须匹配。