Solved

Desserialize a Custom Class that had its structure changed

Posted on 2010-09-22
4
337 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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

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.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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