471,075 Members | 715 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Issues Serializing A Derived Class and its Base using IXmlSerializable, Can't See Base

Hey All,

I am having issue with serializing a class and its base class using
..net 2.0. I am using IXmlSerializable

I've provided code displaying my at the bottom of this post. Thanks
ahead of time for any help or feedback.


I am using System.Xml.Serialization I to serialize a class.
Specifically, I am having the class inherit IXmlSerializable and
overiding the serialize and deserialize methods so that i have complete
control over how my xml is rendered. Works great!

Consider the following secnario:
* There exists a class called Person
* Person contains info like Age & Name
* Person inherits IXmlSerializable
* There exists a class called Employee
* Employee inherits Person as its base class
* Employee contains info like Title
* Person inherits IXmlSerializable

When i serialize an instance of Person it works fine. BUT when i
serialize and instance of Employee, I am only able to serialize
employee Title, not Age or Name as derived from Person.

Now this should work, i should be able to serialize the base class from
the derived without explicitly doing so. I say this because when i do
not use IXmlSerializable and just let XmlSerializer drive in terms of
interpreting schema and structure of a class, it is able to serialize
the derived and base class members.

1. Inheriting IXmlSerializable for both a base and derive class, am i
able to easily serialize a derived and base classs info from the
derived class.

CODE -- As described in Problem section above
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.Xml;

namespace Test{
public class Person : IXmlSerializable
private int m_age;
private string m_name;

public int Age
get { return m_age; }
set { m_age = value; }
public string Name
get { return m_name; }
set { m_name = value; }

#region IXmlSerializable Members

System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
return null;

void IXmlSerializable.ReadXml(XmlReader reader)
this.Age = Convert.ToInt32(reader.ReadString());

this.Name = reader.ReadString();

void IXmlSerializable.WriteXml(XmlWriter writer)
writer.WriteElementString("Age", this.Age.ToString());
writer.WriteElementString("Name", this.Name);


}//end class
public class Employee : Person, IXmlSerializable {
private string m_title;

public string Title
get { return m_title; }
set { m_title = value; }
#region IXmlSerializable Members

System.Xml.Schema.XmlSchema IXmlSerializable.GetSchema()
return null;

void IXmlSerializable.ReadXml(XmlReader reader)
this.Title = reader.ReadString();


void IXmlSerializable.WriteXml(XmlWriter writer)
writer.WriteElementString("Title", this.Title);



class Test
static void Main(string[] args)

Employee employee= new Employee();
employee.Title = "Boss";
employee.Name = "Mr. Man";
employee.Age = 16;
XmlSerializer xmlSerialize = new

StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
settings.Indent = true;

XmlWriter writer = XmlTextWriter.Create(sb, settings);
xmlSerialize.Serialize(writer, employee);

Console.WriteLine( "\n\n\n" + sb.ToString());

Employee newEmployee
= (xmlSerialize.Deserialize(
) as Employee);

}//end main
}//end class
}//end namespace

Feb 10 '06 #1
1 6007
I believe i solved the issue.

I just had to make my base class IXmlSerializable methods virtual and
my derive class IXmlSerializable methods override.

Feb 10 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Aleksei Guzev | last post: by
1 post views Thread by Aidan Glendye | last post: by
reply views Thread by Redowl | last post: by
reply views Thread by Ignace 'a0a' Saenen | last post: by
reply views Thread by leo001 | last post: by

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.