Re: XmlSerialization base classes
- From: "Chris Szabo" <ChrisSzabo@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Fri, 20 Jan 2006 08:19:04 -0800
Kevin, thanks for getting back to me. I assumed that the inheritance chain
was important because of who the XmlSerializer works. Let me include some of
the code:
[XmlRoot ( "Person", IsNullable = true, Namespace = "" )]
[XmlInclude ( typeof ( Student ) )]
public class Person
{
/// <summary>
/// Provides protected access to private member _IsDirty to derived objects.
/// </summary>
[ XmlIgnore ( )]
public bool IsDirty
{
get { return _IsDirty; }
}
/// <summary>
/// Provides public access to the private
/// member _Id.
/// </summary>
[XmlAttribute ( )]
public int Id
{
get { return _Id; }
set
{
_Id = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to the private
/// member _FirstName.
/// </summary>
[XmlAttribute ( )]
public string FirstName
{
get { return _FirstName; }
set
{
_FirstName = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to the private
/// member _LastName.
/// </summary>
[XmlAttribute ( )]
public string LastName
{
get { return _LastName; }
set
{
_LastName = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to the private
/// member _MiddleInitial.
/// </summary>
[XmlAttribute ( )]
public string MiddleInitial
{
get { return _MiddleInitial; }
set
{
_MiddleInitial = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to the private
/// member _AddressInfo.
/// </summary>
public Address [ ] AddressInfo
{
get { return _AddressInfo; }
set
{
_AddressInfo = value;
_IsDirty = true;
}
}
#endregion
#region Constructors
/// <summary>
/// Default constructor. Creates a new instance with no parameters.
/// </summary>
public Person ( ) { }
#endregion
}
[ XmlRoot ( "Student", IsNullable = true, Namespace = "" ) ]
public class Student : Person
{
#region Properties
/// <summary>
/// Provides protected access to private member _IsDirty to derived objects.
/// </summary>
[XmlIgnore]
protected new bool IsDirty
{
get { return _IsDirty; }
}
/// <summary>
/// Provides public access to the private memeber _StudentId.
/// </summary>
[ XmlAttribute ( ) ]
public int StudentId
{
get { return _StudentId; }
set
{
_StudentId = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to private member _Courses.
/// </summary>
public AssignedCourse [ ] CourseCollection
{
get { return _CourseCollection; }
set
{
_CourseCollection = value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to private member _Status.
/// </summary>
[XmlIgnore]
public Prototype.Enumeration.StudentStatus CurrentStatus
{
get { return _Status; }
set
{
_Status = value;
_IsDirty = true;
}
}
[XmlAttribute ( )]
public int Status
{
get { return ( int ) _Status; }
set
{
_Status = ( Prototype.Enumeration.StudentStatus ) value;
_IsDirty = true;
}
}
/// <summary>
/// Provides public access to private member _Gpa.
/// </summary>
[XmlAttribute ( )]
public float Gpa
{
get { return _Gpa; }
set
{
_Gpa = value;
_IsDirty = true;
}
}
#endregion
/// <summary>
/// Default constructor. Creates a new instance with no parameters.
/// </summary>
public Student ( ) : base ( ) { }
}
Each property references a private field (not included in post). The reason
I used the particular overload is because Student is derived from Person...
according to the documentation:
"You can also use the extraTypes parameter to specify types derived from a
base class. For example, suppose a base class named Phone exists, and a class
named InternationalPhone derives from it. Use the extraTypes parameter to
specify the derived type as well."
Any ideas on what might be wrong?
"Kevin Spencer" wrote:
> I'm not sure why you're using the particular overload you're using, but that
> is not necessarily important.
>
> Also, the fact that the Student class is derived from the Person class is
> unimportant. Remember that all classes, except for Object, are derived, at
> least from Object.
>
> Properties that are serialized must have a getter and a setter. In addition,
> the properties themselves must be serializable. Classes that implement
> IEnumerable must implement a public Add method to be serializable. Classes
> that implement ICollection must implement a public Item indexer. If any
> members of your Student class are of these types, make sure that they can be
> serialized.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> Who is Mighty Abbott?
> A twin turret scalawag.
>
> "Chris Szabo" <ChrisSzabo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
> news:E6F01EC9-43DD-4F9A-B951-A77AD8885584@xxxxxxxxxxxxxxxx
> > Good afternoon everyone. I'm running into a problem deserializing a
> > stream
> > using the XmlSerializer. A stored procedure returns the following from
> > SQL
> > Server:
> >
> > <Student StudentId="1" Status="1" Gpa="3.50">
> > <Person Id="1" FirstName="FirstName0" LastName="LastName0"
> > MiddleInitial="W"/>
> > </Student>
> >
> > In my code, person is the base class and student extends it. When I
> > instantiate the XmlSerializer I use the following overload: XmlSerializer
> > (
> > Type, Type [ ] ). The first parameter is the derived type, Student. The
> > second parameter, the Type [ ] contains the base type, Person.
> >
> > The student information deserializes correctly into a student object.
> > However, all of the base properties are not populated. I have tried using
> > the XmlInclude decoration on the base class, directing it to include
> > Student
> > as a viable type. That also has not worked. All of the properties in
> > both
> > classes are public, and if I try to deserialize an instance of Person from
> > Xml it works perfectly.
> >
> > Has anyone run into this before? Does anyone know how to get around the
> > problem sort of implementing IXmlSerializable on the derived class?
> >
> > Thanks everyone.
> >
>
>
>
.
- Follow-Ups:
- Re: XmlSerialization base classes
- From: Kevin Spencer
- Re: XmlSerialization base classes
- References:
- Re: XmlSerialization base classes
- From: Kevin Spencer
- Re: XmlSerialization base classes
- Prev by Date: Re: Ini file equivalent ASP.Net etc
- Next by Date: Re: Ini file equivalent ASP.Net etc
- Previous by thread: Re: XmlSerialization base classes
- Next by thread: Re: XmlSerialization base classes
- Index(es):
Relevant Pages
|