Solved

Desserialize a Custom Class that had its structure changed

Posted on 2010-09-22
4
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Error import assembly 19 27
Set arraylist as datatable 2 29
C# DateTime Help 6 46
RestClient.cs fails at (HttpWebResponse)request.GetResponse()) 4 52
In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

738 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