2009-08-31 21 views
3

いくつかのオブジェクトをシリアル化するためにSOAPを使用している既存のリモーティングコードと下位互換性が必要なコードを書いています。.NET SoapFormatterで名前空間を制御する方法は?

私の難しさは、いくつかのオブジェクトを新しいアセンブリに移動しなければならなかったため、リモーティングが壊れていることです。

例えば、私はこのような.NET SoapFormatterを使用してオブジェクトをシリアライズ:

Person p=new Person(); 
string [email protected]"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は次のようになります。その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> 

、私がしたいのですがA1上のxmlnsをある程度制御:Personオブジェクト:

<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を手動で変更しない方がよいでしょう。

答えて

2

SoapType属性を使用して名前空間を設定できました。

[Serializable] 
[System.Runtime.Remoting.Metadata.SoapType(XmlNamespace = "MY_NAMESPACE")] 
public class Person 
{ 
    public string FirstName; 
    public string LastName; 
    public string _Address; 
    public string _ZIPCode; 
} 


これは、次のシリアル化された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> 
    <i2:Person id="ref-1" xmlns:i2="MY_NAMESPACE"> 
     <FirstName id="ref-3">John</FirstName> 
     <LastName id="ref-4">Doe</LastName> 
     <_Address id="ref-5">otneat.net Street, Zaragoza, Spain</_Address> 
     <_ZIPCode id="ref-6">50007</_ZIPCode> 
    </i2:Person> 
    </SOAP-ENV:Body> 
</SOAP-ENV:Envelope> 


しかし、私はリモートチャネルの反対側に直列化復元を確認することができませんでした。うまくいけば、それはあなたを助けます。

+0

これを試してみます。私はこれを試みたと思ったが、私はしたかもしれない:[SoapType(NameSpace = "MY_NAMESPACE")、XmlNameSpaceの代わりに。ショットに値する、ありがとう。 –

+1

XmlNameSpace = ...がキーです。ありがとう。 –

0

SoapAttributeを試しましたか? XmlNamespaceプロパティがあります。それはうまくいくかもしれません。 Reflectorを使用して、SoapFormatter.Serializeが何をしているかを見ることもできます。あなたが試みることができるいくつかのアイデアを得るかもしれません。

関連する問題