Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# XML Serial
Hey guys,
 
I've got a question, but I'm pretty sure the answer is no. But is it possible to change the type of a property during serialization.
 
For example:
 
If you have a bool property but you wanted to save it as a string in an xml file (for readability purposes). Is this possible?
 
I've not produced any sample code yet as it is just a theoretical approach at this time. I've done some googling but I've not found any results that say it is/isn't possible so I thought I would ask.
Posted 10-Apr-13 13:28pm
Pheonyx11.9K
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

You are pretty much right about "no", but this is not really related to serialization. You cannot change the declaration of a type during run-time. This fact is more fundamental than serialization. I don't know why would you want it (that's why you are advised to tell us your ultimate goals); I think the whole idea is based on some misconception.
 
Strictly speaking, you can always change the type of the property in certain sense: if the declared (compile-time) type of the property is some class which has descendant classes, the run-time type of the property could be one or another of those descendant classes. This is a general principle of OOP. However, this aspect is trivial; and I'm sure you understand it. Anyway, you cannot change Boolean to string, or anything like that, anything beyond assignment compatibility.
 
—SA
  Permalink  
Comments
Pheonyx at 11-Apr-13 4:18am
   
Hi Sergey,
 
Thanks for that. I thought it was the case. Another question on the same topic (save making a new thread). Is it possible to have a class with a property that is ONLY used during serialization and cannot be accessed through normal code?
 
Basically, I am using a "SecureString" to store a password in a settings class. However the password does need to be outputted to a configuration file. I understand this defeats the purpose of the SecureString class, but I want to serialize an encrypted version of the
SecureString. I only want this property to be available for the serialization and not as a normal property (if that makes sense).
Sergey Alexandrovich Kryukov at 11-Apr-13 11:45am
   
No. There is no such mechanism. Basically, one of the approaches is this: you need to customize serialization itself. Make one property serializable, another non-serializeable. Pre-process the instance of the class before serialization to convert data to serializeable form (encrypt, or whatever), post-process after deserialization.
 
Are you using Data Contract? If either XML or JSON is fine, drop everything and switch to Data Contract. It has appropriate methods you can implement and include in this mechanism using appropriate attributes.
 
If you need more help, please remind me by replying to this comment.
 
—SA
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

A boolean value is serialised to "false" or "true" - is it this what you want to change? - It does not work.
If you want to change the name of the property, you can use the XmlElementAttribute:
[System.Xml.Serialization.XmlElementAttribute("This_is_the_text_you_want_to_show")]
public bool MyIncomprehensibleProperty
{ ... }
  Permalink  
Comments
Pheonyx at 11-Apr-13 4:22am
   
Hi Bernhard,
 
That wasn't quite what I was trying to get at I'm afraid. I used Bool as an example, but in reality I am trying to serialize a "SecureString" which I know is bad practice but is required. I want to secure it as a string that will be encrypted and wondered if you could convert during serialization.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

In principle, you may always write you own serialization stuff (please note: writing a reliable serialization facility for all but very simple cases it is a quite difficult task): the most important property of serialization is reversibility: if you serialize an object then you have to be sure you can get it back to life. Provided your serialization code warrants reversibility, it can store object state in the form it likes (you like) more.
  Permalink  
Comments
Pheonyx at 11-Apr-13 4:23am
   
Hi CP,
 
Thanks for your response, at this stage looking to write my own serializations stuff is a bit to much for me. I wouldn't know where to start and to do it well I don't really have the time. I will bare this in mind in future though. Thanks.
Sergey Alexandrovich Kryukov at 11-Apr-13 11:47am
   
No, why? it's really easy to just customize serialization using available mechanism. I do such things with Data Contract. (Please see my comment to my answer.).
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 190
1 Jochen Arndt 165
2 PIEBALDconsult 150
3 BillWoodruff 148
4 DamithSL 140
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


Advertise | Privacy | Mobile
Web03 | 2.8.141220.1 | Last Updated 11 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100