Solved

Desserialize a Custom Class that had its structure changed

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

758 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

23 Experts available now in Live!

Get 1:1 Help Now