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

This is a discussion on Issues Serializing A Derived Class and its Base using IXmlSerializable, Can't See Base - XML SOAP ; 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 ...

+ Reply to Thread
Results 1 to 2 of 2

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

  1. Default 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


  2. Default Re: Issues Serializing A Derived Class and its Base using IXmlSerializable, Can't See Base

    I believe i solved the issue.

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


+ Reply to Thread

Similar Threads

  1. Base class parameters called on Derived class: behaviour
    By Application Development in forum c++
    Replies: 3
    Last Post: 11-28-2007, 04:09 PM
  2. operator=() in base and derived class
    By Application Development in forum c++
    Replies: 7
    Last Post: 10-23-2007, 02:07 AM
  3. Replies: 2
    Last Post: 06-03-2007, 10:00 AM
  4. Replies: 7
    Last Post: 04-02-2007, 04:53 AM
  5. Force a derived class to Serialize/Serialise as its base class
    By Application Development in forum XML SOAP
    Replies: 0
    Last Post: 01-30-2007, 02:01 PM