Re: XmlSerialization base classes

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



Hi Chris,

> I assumed that the inheritance chain
> was important because of who the XmlSerializer works.

You may be right. Turns out I've serialized inherited classes, but I wrote
custom serializers. I've also used XmlSerializer, but not on inherited
classes! So I can't be sure, now that I've looked back at my previous
experiences.

So, at this point I can tell you this much from my personal experience, and
from the example in the SDK. I know that I have had problems in the past
with over-using and mis-using XML attributes. I noticed that the only XML
attribute in the example was for the object array in the Teacher class. For
the most part, the XmlSerializer doesn't need attributes to do
serialization. They are available for customized serialization, but the
XmlSerializer uses Reflection for almost everything it does. You might try
removing most of the attributes you've added, and see how that works out.
Take another look at the example.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.


"Chris Szabo" <ChrisSzabo@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:BFB6F1FC-BF0C-44D3-AD76-6E091E6DBD4C@xxxxxxxxxxxxxxxx
> 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.
>> >
>>
>>
>>


.



Relevant Pages

  • Re: XmlSerialization base classes
    ... was important because of who the XmlSerializer works. ... /// Provides public access to the private ... I used the particular overload is because Student is derived from Person... ... named InternationalPhone derives from it. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: XML Serialization
    ... serialization offers in that you can serialize private members. ... the biggest problem we have with XMLSerializer is that our ... DataContractSerializer could serialize private members and/or properties. ... We have an app that uses XML serialization throughout. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: class object serialization
    ... i have tried XMLserializer in window application, i m using code below to ... XmlSerializer serial = new XmlSerializer); ... i m using simple class to test the serialization, ... private string _Fname; ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: class object serialization
    ... i have tried XMLserializer in window application, i m using code below to ... XmlSerializer serial = new XmlSerializer); ... i m using simple class to test the serialization, ... private string _Fname; ...
    (microsoft.public.dotnet.framework.compactframework)
  • xml serialize private properties with a wrapper class -- how to?
    ... We're using XmlSerializer to serialize our data objects for ... persistence in a WinForms app. ... We just came across a note in one of the articles on XML Serialization ...
    (microsoft.public.dotnet.languages.vb)