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

P

Peter Nofelt

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.

Cheers,
Peter
www.nofelt.com

Situation
===========
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!

Problem
===========
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.

Questions
============
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)
{
reader.ReadToDescendant("Age");
this.Age = Convert.ToInt32(reader.ReadString());

reader.ReadToNextSibling("Name");
this.Name = reader.ReadString();
}

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

#endregion

}//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)
{
reader.ReadToDescendant("Title");
this.Title = reader.ReadString();

}

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

#endregion

}

class Test
{


static void Main(string[] args)
{

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


XmlSerializer xmlSerialize = new
XmlSerializer(typeof(Employee));

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

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

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

///////Deserialize
Employee newEmployee
= (xmlSerialize.Deserialize(
XmlTextReader.Create(new
StringReader(sb.ToString()))
) as Employee);

}//end main
}//end class
}//end namespace
=============================
END OF CODE
 
P

Peter Nofelt

I believe i solved the issue.

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top