Solved

Desserialize a Custom Class that had its structure changed

Posted on 2010-09-22
4
323 Views
Last Modified: 2012-06-27
Hi experts.

Suppose that I have a Class that defines a Product in my catalog. This class have 2 attributes: Product_Code and Product_Description, both as string.

A instance of this class was serialized as XML and saved to the disk.

Now, suppose that a new attribute, called Product_Supplier was added to the class. What will happen if I try to deserialize the XML file that was previously saved? Is there a way to perform this action sucessfully, including when attributes are deleted or have it's names changed?

Thanks a lot.
0
Comment
Question by:regisdaniel
4 Comments
 
LVL 55

Assisted Solution

by:Jaime Olivares
Jaime Olivares earned 150 total points
ID: 33740640
There is no problem is a property is missing in the xml. It will keep the default or existent value.
If the attribute name is changed then you will have a problem with deserialization.
If you want to handle this kind of situation, you can do custom serialization and ignore not-known tags.
0
 
LVL 2

Assisted Solution

by:DmitryVasiliev
DmitryVasiliev earned 150 total points
ID: 33741358
You can use SoapAttribure and SoapElement parameters to deserialize fields with changed name

[SoapAttribute(DataType = "date", AttributeName = "CreationDate")]
[SoapElement(DataType = "nonNegativeInteger", ElementName = "PosInt")]

See details here: http://msdn.microsoft.com/en-us/library/system.xml.serialization.soapattributeattribute(v=VS.90).aspx
0
 
LVL 16

Accepted Solution

by:
kris_per earned 200 total points
ID: 33748604

By default,
if new attribute added in class: old xml will serialize fine; new attribute field in class will be left untouched with the default value.

if an attribute is deleted in class: old xml will serialize fine; If you still want to check/process the old value, then you can use UnknownAttribute event handler of XmlSerializer (or UnknownNode event for nodes)

if attribute name changed: by default this is like an attribute with old name is deleted and a new attribute is added. But you can force attribute name in xml whatever the field name is using XmlAttribute attribute like:
[XmlAttribute(AttributeName = "CreationDate")]
public DateTime Today; // though field name is Today, attribute in xml will be CreationDate


For more details, check out this msdn link => http://msdn.microsoft.com/en-us/library/system.xml.serialization.xmlserializer.aspx
0
 

Author Closing Comment

by:regisdaniel
ID: 33809074
Thanks a lot!!!
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now