473,385 Members | 1,356 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

XML Serialization of derived classes

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
6 6029
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Brad Kartchner | last post by:
I'm attempting to write a program with several child classes derived from a single parent class. The parent class has a couple of variables that need to be present in each of the child classes. ...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
5
by: Nathan Bullock | last post by:
Hi, I have a base class, say Base and there are two classes, say Class1 and Class2 which are derived from Base. Is there any way for me, say from a static method in Base, to get a list of all...
13
by: z. f. | last post by:
Hi, i have a class that is derived from System.Web.UI.Page, and this is the class i use in my application as PageBase. all other page classes are deriverd from my PageBase instead of the...
8
by: Manuel | last post by:
Hi! If I've a vector filled with abstract classes, can I push in it the derived classes too? Even if derived classes have new methods? I've done some experiments, and it seem I can push the...
2
by: Jinsong Liu | last post by:
I have a class which serve as a base class. I want to focuses all the classes derived from it implement a interface. Make it a abstract class is not a option since some XML serialization code need...
6
by: ivan.leben | last post by:
I want to write a Mesh class using half-edges. This class uses three other classes: Vertex, HalfEdge and Face. These classes should be linked properly in the process of building up the mesh by...
15
by: Bob Johnson | last post by:
I have a base class that must have a member variable populated by, and only by, derived classes. It appears that if I declare the variable as "internal protected" then the base class *can*...
0
by: Shaul | last post by:
Hi, My goal is to serialize a collection of type IList<AbstractClass> which contains derived objects instances. My domain object model is a bit complex so I've created a demmi one: public...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.