473,624 Members | 2,119 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Issues Serializing A Derived Class and its Base using IXmlSerializabl e, 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 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

Feb 10 '06 #1
1 6360
I believe i solved the issue.

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

Feb 10 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
2486
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 derived classes from the abstract classes The 1st class contains the 2nd class The 2nd class is a custom collection for the 3rd class
2
3289
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 ‘DataItem’ public class DataItem { public DataItem() {}
1
2164
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 contain Exceptions, so I've decided to implement IXmlSerializable to get round the fact that Exception is not serializable.
1
2574
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 session. All of the objects are classes which inherit from a typed dataset. Looking at MSDN I have found the following two (what I believe to be) relevent statements (from article Object
9
1794
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 figure out what attribute to put on the base class method to prevent this. TIA, Larry Woods
0
1002
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 the derived class, the value for this property is lost. Grateful for any help. Alex
1
831
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 of time for any help or feedback. Cheers, Peter
2
697
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 UserControl are like that). When serializing, how could I ignore all the properties coming from the UserControl class? I know there is XmlIgnoreAttribute, but how could I set it to every property of UserControl, as it is not my class? Thank you...
2
3555
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 in .NET, is there a known workaround? using System;
0
1068
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 XmlSerializer ctor's to specify base class A and derived class B relationships. However, XmlInclude only works at compile-time, and I would like to support serialisation for future derived classes C in other modules. So in this case, this option...
0
8234
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
8335
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8474
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6110
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5563
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4079
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4174
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1784
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1482
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.