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.

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

.



Relevant Pages

  • Issues Serializing A Derived Class and its Base using IXmlSerializable, Cant See Base
    ... I am having issue with serializing a class and its base class using ... I am using System.Xml.Serialization I to serialize a class. ... There exists a class called Employee ... void IXmlSerializable.WriteXml ...
    (microsoft.public.dotnet.xml)
  • RE: serialize/deserialize object into string
    ... it seriliaze an object to a string.. ... public class Employee ... Create a new MemoryStream class with the CanWrite property set to true ... Serialize method of the BinaryFormatter class. ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: XML Serialization
    ... I can't use dataset as there is a hierarchy within the objects. ... Employee will have a list of addresses etc.. ... So I construct the object graph and then serialize the object ... Rjn ...
    (microsoft.public.dotnet.languages.vb)
  • RE: Protected keyword
    ... Only the Employee class internally can see the base class RaiseEvent method. ... public void HoldBreath{ ... a child that inherits from it so as to be able to call the protected method ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How to prevent base class (UserControl) form being serialized
    ... the base class so your control won't have a name, know its visibility, know ... My object happens to be derived from a control so I am penalizes for that. ... them on the code for the UserControl. ... Just serialize this structure ...
    (microsoft.public.dotnet.general)

Quantcast