【发布时间】:2009-08-31 20:35:51
【问题描述】:
我正在编写一些代码,这些代码需要与使用 SOAP 序列化某些对象的现有远程处理代码向后兼容。
我的困难是我不得不将一些对象移动到新的程序集中,所以远程处理被破坏了。
例如,我使用 .NET SoapFormatter 序列化一个对象,如下所示:
Person p=new Person();
string path=@"c:\myfile.soap";
using (System.IO.FileStream fs = new System.IO.FileStream(path, System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write))
{
System.Runtime.Serialization.Formatters.Soap.SoapFormatter
f = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();
f.Serialize(fs, p);
fs.Close();
}
生成的 xml 如下所示:
<SOAP-ENV:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:clr="http://schemas.microsoft.com/soap/encoding/clr/1.0" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<a1:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/dotneat_net.Serialization%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
<FirstName id="ref-3">Joe</FirstName>
<LastName id="ref-4">Doe</LastName>
<_Address id="ref-5">dotneat.net Street, Zaragoza, Spain</_Address>
<_ZIPCode id="ref-6">50007</_ZIPCode>
</a1:Person>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
在那个 XML 中,我想对 a1:Person 对象上的 xmlns 进行一些控制:
<a1:Person id="ref-1" xmlns:a1="http://schemas.microsoft.com/clr/nsassem/Serialization/dotneat_net.Serialization%2C%20Version%3D0.0.0.0%2C%20Culture%3Dneutral%2C%20PublicKeyToken%3Dnull">
原因是我的新 Person 对象与原始对象不在同一个程序集中。因此,稍后在发生反序列化时(在较旧的项目中),由于组装错误,它会失败。
如何控制 xmlns 中的文本?我尝试了一些方法,例如在被序列化的类上使用 [SoapType Namespace="xxx"] 属性。没有运气。
我宁愿避免手动修改 XML。
【问题讨论】:
标签: c# serialization soap