Hey All,
I am having issue with serializing a class and its base class using
..net 2.0. I am using IXmlSerializabl e
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.Seri alization I to serialize a class.
Specifically, I am having the class inherit IXmlSerializabl e 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 IXmlSerializabl e
* There exists a class called Employee
* Employee inherits Person as its base class
* Employee contains info like Title
* Person inherits IXmlSerializabl e
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 IXmlSerializabl e 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 IXmlSerializabl e 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.Collecti ons.Generic;
using System.Text;
using System.Xml.Seri alization;
using System.Xml;
namespace Test{
public class Person : IXmlSerializabl e
{
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 IXmlSerializabl e Members
System.Xml.Sche ma.XmlSchema IXmlSerializabl e.GetSchema()
{
return null;
}
void IXmlSerializabl e.ReadXml(XmlRe ader reader)
{
reader.ReadToDe scendant("Age") ;
this.Age = Convert.ToInt32 (reader.ReadStr ing());
reader.ReadToNe xtSibling("Name ");
this.Name = reader.ReadStri ng();
}
void IXmlSerializabl e.WriteXml(XmlW riter writer)
{
writer.WriteEle mentString("Age ", this.Age.ToStri ng());
writer.WriteEle mentString("Nam e", this.Name);
}
#endregion
}//end class
public class Employee : Person, IXmlSerializabl e {
private string m_title;
public string Title
{
get { return m_title; }
set { m_title = value; }
}
#region IXmlSerializabl e Members
System.Xml.Sche ma.XmlSchema IXmlSerializabl e.GetSchema()
{
return null;
}
void IXmlSerializabl e.ReadXml(XmlRe ader reader)
{
reader.ReadToDe scendant("Title ");
this.Title = reader.ReadStri ng();
}
void IXmlSerializabl e.WriteXml(XmlW riter writer)
{
writer.WriteEle mentString("Tit le", 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(t ypeof(Employee) );
StringBuilder sb = new StringBuilder() ;
XmlWriterSettin gs settings = new XmlWriterSettin gs();
settings.OmitXm lDeclaration = true;
settings.Indent = true;
XmlWriter writer = XmlTextWriter.C reate(sb, settings);
///////Serialize
xmlSerialize.Se rialize(writer, employee);
Console.WriteLi ne( "\n\n\n" + sb.ToString());
///////Deserialize
Employee newEmployee
= (xmlSerialize.D eserialize(
XmlTextReader.C reate(new
StringReader(sb .ToString()))
) as Employee);
}//end main
}//end class
}//end namespace
=============== ==============
END OF CODE