473,396 Members | 2,010 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 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.

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

Feb 10 '06 #1
1 6316
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: big A | last post by:
I am receiving an error stating that File or Assembly name <filname.dll>, or one of its dependencies, was not found In one assembly I have three abstract classes In another I have three...
2
by: Aleksei Guzev | last post by:
Imagine one writing a class library CL1 for data storage. He defines classes ‘DataItem’ and ‘DataRecord’ so that the latter contains a collection of the former. And he derives class ‘IntItem’ from...
1
by: mattoc | last post by:
Happy new year to all. I have a strange error that I've been trying for a while now to fathom.. Basically I have a hierarchy of state classes that I need to serialize to XML. Some of them can...
1
by: Aidan Glendye | last post by:
Hi, I am trying to store various objects within session. Due to the site being hosted in a web farm we are going to have to use either State Server or SQL Server to persist the data in...
9
by: Larry Woods | last post by:
I have a method in my base class that I want ALL derived classes to use. But, I find that I can create a "Shadow" method in my derived class that "overrides" the method in my base class. Can't...
0
by: Redowl | last post by:
Apologies if this has been covered in an early question, but I am having difficulty serializing a derived class. The base class has a property which is marked as MustOverride, but when I serialize...
1
by: Peter Nofelt | last post by:
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...
2
by: she_prog | last post by:
I have a class derived from UserControl. I need to serialize an object of this class, but only some properties of it, as not all properties are serializable (some of the properties coming from...
2
by: ilitirit | last post by:
Can anyone explain why the following program doesn't work? The attributes and elements of the MessageList class are not being generated. Am I doing something incorrectly? Or if this is a bug...
0
by: Ignace 'a0a' Saenen | last post by:
Hi, I have actually found 2 ways to do this. One uses the XmlInclude attribute in the base class A, specifying the valid derived types to deserialize from the stream, and one uses one of the 8...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.