By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,081 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,081 IT Pros & Developers. It's quick & easy.

XML Serialization of derived classes

P: n/a
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
Dec 9 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi John,

There is something wrong with your derived class code. The current code
you're providing is NOT deriving from MyBaseClass. You have to add
:MyBaseClass after the declaration of the derived class like the following.
Then it will work fine.

public class MyDerivedClass {
public MyDerivedClass ( ) : base( ) { }

private string myNewVariable;

public string MyNewVariable {
get { return myNewVariable; }
set { myNewVariable = value; }
}
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 9 '05 #2

P: n/a
Thanks Kevin,

That's not the problem. I simply missed that piece when I pasted it in.
But I take it that you must be able to get this to work properly or you
wouldn't have suggested this as the reason it wasn't working.

Thanks,
John Glover

"Kevin Yu [MSFT]" wrote:
Hi John,

There is something wrong with your derived class code. The current code
you're providing is NOT deriving from MyBaseClass. You have to add
:MyBaseClass after the declaration of the derived class like the following.
Then it will work fine.

public class MyDerivedClass {
public MyDerivedClass ( ) : base( ) { }

private string myNewVariable;

public string MyNewVariable {
get { return myNewVariable; }
set { myNewVariable = value; }
}
}

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 9 '05 #3

P: n/a
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

Dec 9 '05 #4

P: n/a
Yes, John. After I added : MyBaseClass to the code, the returned message
looks like this:

<?xml version="1.0" encoding="utf-8" ?>
<MyDerivedClass xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://tempuri.org/">
<MyVariable>old</MyVariable>
<MyNewVariable>new</MyNewVariable>
</MyDerivedClass>

Seems to be fine.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Dec 10 '05 #5

P: n/a
DC
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

Dec 13 '05 #6

P: n/a
Did you get this sorted ?

I think I am having a similar problem to your original question. I can't
seem to serialize overriden properties of a derived class. Sorry to jump on
your question but would be very grateful for any advice.

"DC" wrote:
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


Dec 13 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.