4

I am writing some code that needs to be backwards compatible with EXISTING remoting code that is using SOAP to serialize some objects.

My difficulty is that I have had to move some objects to new assemblies, so the remoting is broken.

For example, I serialize an object using the .NET SoapFormatter like this:

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();
}

The resulting xml looks like this:

<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>

In that XML, I'd like to have some control over the xmlns on the a1:Person object:

<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">

The reason is that my new Person object is not in the same assembly as the original object. So, later when deserialization occurs (in the older projects), it fails because of wrong assembly.

How can I control the text in the xmlns? I have tried a few things such as using the [SoapType Namespace="xxx"] attribute on the class that is being serialized. No luck.

I'd prefer to avoid modifying the XML manually.

3 Answers 3

3

I was able to set the namespace using the SoapType attribute.

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


This generated the following serialized 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>


However, I wasn't able to verify the deserialization on the other side of the remoting channel. Hopefully, that helps you.

4
  • I will give this a try. I thought I tried this, but I may have done: [SoapType(NameSpace="MY_NAMESPACE"), instead of XmlNameSpace. Worth a shot, thanks.
    – jm.
    Sep 1, 2009 at 14:17
  • 1
    XmlNameSpace=... was the key. Thanks.
    – jm.
    Sep 3, 2009 at 18:32
  • Funky that <a1:Person id="ref-1" became <i2:Person id="ref-1"
    – malat
    Oct 13, 2022 at 11:54
  • If you are using .NET note that, SoapType in System.Runtime.Remoting.Metadata namespace has internal access level. You need to use SoapType in System.Xml.Serialization namespace instead. Jun 29 at 18:49
0

Have you tried SoapAttribute? It has an XmlNamespace property. May be that will work. You can also use Reflector to see what SoapFormatter.Serialize is doing. You may get some ideas that you can try.

0

While the solution from Randy does work as expected. I did want to preserve the original a1 notation:

<a1:Person id="ref-1" 

So I ended up implementing:

[Serializable]
public class Person : System.Runtime.Serialization.ISerializable
{
    public string FirstName;
    public string LastName;
    public string _Address;
    public string _ZIPCode;

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AssemblyName = "ConsoleApp";
        System.Reflection.MemberInfo[] members = this.GetType().GetMembers();
        foreach (var member in members)
        {
            if (member.MemberType == System.Reflection.MemberTypes.Field)
            {
                System.Reflection.FieldInfo field = ((System.Reflection.FieldInfo)member);
                string name = member.Name;
                object value = field.GetValue(this);
                info.AddValue(name, value);
            }
        }
    }
}

Turns out there is an even easier solution:

SoapFormatter soapFormatter = new SoapFormatter();
soapFormatter.AssemblyFormat = FormatterAssemblyStyle.Simple;
soapFormatter.Serialize(fs, p);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.