Groups | Blog | Home
all groups > dotnet xml > july 2004 >

dotnet xml : Controlling SOAP formatting


Bob Rundle
7/18/2004 10:10:32 AM
I can't seem to control the soap formatting. SoapTypeAttribute() doesn't
seem to have any affect on the SOAP serialization. Here is my sampe code.
What am I doing wrong?

Regards,
Bob Rundle

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace DotnetBench
{
/// <summary>
/// Summary description for Class1.
/// </summary>
class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string path = "c:\\scratch\\zxcv.xml";
MyClass a = new MyClass();
try
{
Stream sin = new FileStream(path,FileMode.Create);
SoapFormatter sfin = new SoapFormatter();
sfin.Serialize(sin,a);
sin.Close();
}
catch(Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex);
}
}
}
}

[Serializable]
[SoapType("YourClass")]
class MyClass
{
public MyClass() { aprop=123; }
private int aprop;
public int Aprop
{
get { return aprop; }
set { aprop = value; }
}
}

Bob Rundle
7/18/2004 11:31:07 AM
Yes, I saw that in the documentation also. However I have tried many
different combinations all to no avail. The one I posted is simply one
example.

[quoted text, click to view]

Martin Honnen
7/18/2004 5:50:19 PM


[quoted text, click to view]



[quoted text, click to view]


[quoted text, click to view]

I am not an expert on .NET serialization so don't take my answer as
anything being authoritative but looking at the .NET documentation for
SoapTypeAttribute it tells that the attribute is supposed to control the
serialization with XmlSerializer while SoapFormatter is not mentioned
there at all.
An example using XmlSerializer and your class is

using System;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

class Class1
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
string path = "test20040718SOAP.xml";
MyClass a = new MyClass();
try
{
XmlTypeMapping typeMapping = (new
SoapReflectionImporter()).ImportTypeMapping(typeof(MyClass));
Stream sin = new FileStream(path,FileMode.Create);
XmlSerializer soapSerializer = new XmlSerializer(typeMapping);
soapSerializer.Serialize(sin,a);
sin.Close();
}
catch(Exception ex)
{
Console.WriteLine(ex);
}
}
}

[Serializable]
[SoapType("YourClass")]
public class MyClass
{
public MyClass() { aprop=123; }
private int aprop;
public int Aprop
{
get { return aprop; }
set { aprop = value; }
}
}

which results in the following XML

<?xml version="1.0"?>
<YourClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" id="id1">
<Aprop xsi:type="xsd:int">123</Aprop>
</YourClass>

which has the element name YourClass specified by the SoapType
attribute. I realize that that is no SOAP message but that is all I can
contribute, maybe someone else can tell you more about how/whether the
SoapType attribute helps with the SoapFormatter.

--

Martin Honnen
http://JavaScript.FAQTs.com/
AddThis Social Bookmark Button