Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Desserialize a Custom Class that had its structure changed

Posted on 2010-09-22
4
Medium Priority
?
361 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 600 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 600 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 800 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Suggested Courses

772 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