The example included here demonstrates that it works.
-Dino
--
-Dino
D i n o . C h i e s a AT M i c r o s o f t . c o m
--BEGIN CODE--
using System;
using System.Xml.Serialization;
namespace ionic.Xml.Sample
{
public class MyBaseClass {
public MyBaseClass ( ) { }
private string _p;
public string PropertyFromBaseClass {
get { return _p; }
set { _p = value; }
}
}
public class MyDerivedClass : MyBaseClass {
public MyDerivedClass ( ) : base( ) { }
private string _p1;
public string PropertyFromDerivedClass {
get { return _p1; }
set { _p1 = value; }
}
}
public class MyXmlTextWriter : System.Xml.XmlTextWriter {
public MyXmlTextWriter(System.IO.StringWriter w) : base(w) {
Formatting=System.Xml.Formatting.Indented; }
public override void WriteStartDocument () {}
}
/// <summary/>
public class Test
{
static void Main(String[] args)
{
try {
System.IO.StringWriter sw;
XmlSerializer s1= new XmlSerializer(typeof(MyDerivedClass));
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );
MyDerivedClass c= new MyDerivedClass();
c.PropertyFromDerivedClass= "Hallo";
sw = new System.IO.StringWriter();
s1.Serialize(new MyXmlTextWriter(sw), c, ns);
System.Console.Out.WriteLine("Serialize the derived class, with the
\"new\" property set:");
System.Console.Out.WriteLine(sw.ToString() + "\n\n");
System.Console.Out.WriteLine("Serialize the derived class, with the
\"new\" and old properties set:");
c.PropertyFromBaseClass= "Greetings";
sw = new System.IO.StringWriter();
s1.Serialize(new MyXmlTextWriter(sw), c, ns);
System.Console.Out.WriteLine(sw.ToString() + "\n\n");
}
catch (Exception exc1)
{
System.Console.Error.WriteLine("Exception:\n" + exc1);
}
}
}
}
--END CODE--
"John Glover" <jo*********@newsgroups.nospam> wrote in message
news:C8**********************************@microsof t.com...
More information,
So, I decided that I didn't want to trust that this problem always
occurred
with XML Serialization. I had previously been using the built in testing
web
page for my Web Service. So I serialized one of my derived classes
manually
and then printed out the serialization. It appears to work properly. I
have
both properties (MyVariable and MyNewVariable) being printed out. But
this
only leads me to a more disturbing question; Why is this not working with
my
Web Service?
The web page is printing this out for my sample SOAP message:
POST /AskWebService/TestWebService.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://thomson.com/MediaFrame/TestMethod"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<TestMethod xmlns="http://thomson.com/MediaFrame/">
<myClass>
<MyNewVariable>string</MyNewVariable>
</myClass>
</TestMethod>
</soap:Body>
</soap:Envelope>
And the WSDL looks like this:
<wsdl:types>
<s:schema elementFormDefault="qualified"
targetNamespace="http://thomson.com/MediaFrame/">
<s:element name="TestMethod">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="myClass"
type="tns:MyDerivedClass"/>
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="MyDerivedClass">
<s:complexContent mixed="false">
<s:extension base="tns:MyBaseClass">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MyNewVariable"
type="s:string"/>
</s:sequence>
</s:extension>
</s:complexContent>
</s:complexType>
<s:complexType name="MyBaseClass">
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="MyVariable" type="s:string"/>
</s:sequence>
</s:complexType>
<s:element name="TestMethodResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="TestMethodResult"
type="tns:MyBaseClass"/>
</s:sequence>
</s:complexType>
</s:element>
</s:schema>
</wsdl:types>
Is it just the sample SOAP that is incorrect? It seems that the WSDL is
correct, though I may not be reading it right.
Any suggestions?
"John Glover" wrote:
I'm having a very strange problem with XML serialization. I'm writing
web
services which pass instances of various classes back and forth as
parameters
and return values of web methods. The problem is that in my derived
classes,
the XML that is automatically generated is lacking the properties of the
base
class. For example:
public class MyBaseClass {
public MyBaseClass ( ) { }
private string myVariable;
public string MyVariable {
get { return myVariable; }
set { myVariable = value; }
}
}
and
public class MyDerivedClass {
public MyDerivedClass ( ) : base( ) { }
private string myNewVariable;
public string MyNewVariable {
get { return myNewVariable; }
set { myNewVariable = value; }
}
}
This results in XML for the derived class which contains only one element
<MyNewVariable/>, when I wish for it to contain two elements:
<MyVariable/>
and <MyNewVariable/>.
I've tried adding attributes to control the XML Serialization --
[XmlType]
for each of the types, [XmlElement] for each of the properties. I've
also
tried leaving off the [XmlType] attribute for the derived class thinking
that
maybe it was screwing up inheritance. I've also tried (out of
desperation)
[XmlInclude (typeof (MyBaseClass) )] on the derived class. Finally, I
tried
making the variable protected instead of private and then writing a new
property in the derived class and adding the [XmlElement] attribute to
it.
None of these desperate attempts produced any different results.
Can somebody enlighten me as to the correct way to go about this?
Thanks,
John Glover