473,386 Members | 1,699 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Property Type Conversion

Dear All,
Hope somebody can help me with this.

I have created a class and have got it to serialize and deserialize using
XmlSerialization without any problems. I've also read the documentation on
the XML attribute classes such as XmlElement and XmlRoot etc as well as
XmlAttributeOverrides.

However, I run into a problem if I need to perform a conversion on a
property before serializing it. For example, if I have a class with a
property called SomeProperty with a type of Integer. If SomeProperty's value
is 0, I don't want to serialize it but if it's not zero, I want to serialize
it.

Or, another example would be, if I have a class with a property of integer
and I need to serialize the output to XML to convert the value to a string.
ie SomeProperty = 5 and I need the XML output to give me a string "Five"
instead of the default conversion value of "5".

My thinking is that there should be an easy way of applying an attribute to
the property that translates the output of the property to XML and
vice-versa. It would be great if there were an attribute called
XMLConvertAttribute that will specify the conversion to be performed to XML.
But I can't find any information on this. Is there any other easy way?

I know that I could always customize my XML serialization by using
Surrogates or by using IXMLSerializable interface or by just using the quick
and dirty way, by using a function to read all the values in the class and
build the XML by hand. However, all these solutions forces me to customize
serialization for the entire class while all I need is to perform custom
conversion on a couple of properties. Also, I wanted a good object-oriented
method and I just thought somewhere somehow, somebody must have a neat
solution to this problem.

Thanks a lot for any help.
Nov 12 '05 #1
1 1746
Jimmy,

I know what what you're asking for, but unfortunately this functionality is
not available in the .NET framework.

You can achieve the first functionality (selective serialization) with
functionality that's built into the serialization framework to support the
omitting value types from the serialized document.

[XmlIgnore]
public bool MyFieldSpecified;

private int _MyField;
public int MyField
{
get
{
return _MyField;
}
set
{
if( value == 0 )
{
MyFieldSpecified = false;
}
else
{
MyFieldSpecified = true;
}
_MyField = value;
}
}

You can solve the other scenarios you mentioned with properties that perform
where the accessors perform the format conversions, with decorator-like
wrapper classes, or by doing everything by hand and implementing
IXmlSerialziable. Unfortunately there is no easy declarative way to solve
this. Keep asking for it though. Maybe it helps if others are asking for it,
too ;)

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Jimmy Seow" <we*@cawe.com> wrote in message
news:%2****************@TK2MSFTNGP12.phx.gbl...
Dear All,
Hope somebody can help me with this.

I have created a class and have got it to serialize and deserialize using
XmlSerialization without any problems. I've also read the documentation on
the XML attribute classes such as XmlElement and XmlRoot etc as well as
XmlAttributeOverrides.

However, I run into a problem if I need to perform a conversion on a
property before serializing it. For example, if I have a class with a
property called SomeProperty with a type of Integer. If SomeProperty's value is 0, I don't want to serialize it but if it's not zero, I want to serialize it.

Or, another example would be, if I have a class with a property of integer
and I need to serialize the output to XML to convert the value to a string. ie SomeProperty = 5 and I need the XML output to give me a string "Five"
instead of the default conversion value of "5".

My thinking is that there should be an easy way of applying an attribute to the property that translates the output of the property to XML and
vice-versa. It would be great if there were an attribute called
XMLConvertAttribute that will specify the conversion to be performed to XML. But I can't find any information on this. Is there any other easy way?

I know that I could always customize my XML serialization by using
Surrogates or by using IXMLSerializable interface or by just using the quick and dirty way, by using a function to read all the values in the class and
build the XML by hand. However, all these solutions forces me to customize
serialization for the entire class while all I need is to perform custom
conversion on a couple of properties. Also, I wanted a good object-oriented method and I just thought somewhere somehow, somebody must have a neat
solution to this problem.

Thanks a lot for any help.

Nov 12 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
11
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
6
by: D Witherspoon | last post by:
I have a Structure I have created and am using it as a Public Property of a class. Here is the property. ------------------------------------------------------ Dim _MyID As SInteger Public...
5
by: Brent Ritchie | last post by:
Hello, This is my first attempt at template programming so please bear with me. This is what I have so far: Property.h -------------------------- #ifndef PROPERTY_H_ #define PROPERTY_H_
13
by: aegis | last post by:
The following was mentioned by Eric Sosman from http://groups.google.com/group/comp.lang.c/msg/b696b28f59b9dac4?dmode=source "The alignment requirement for any type T must be a divisor of...
9
by: Eric | last post by:
Hi Everyone, I'm writing a UserControl that exposes a property of the type System.Drawing.Image, like this: Public Property DefaultImage() As Image Get Return propDefaultImage End Get...
1
by: --== Alain ==-- | last post by:
Hi, I 'm facing an interesting issue regarding a property and its TypeConverter. When i do not attach a TypeConverter to this property, all custom properties of my custom control are displayed...
2
by: =?Utf-8?B?Z2FkeWE=?= | last post by:
I use one of 2 arrays dependent on the country. Rather than say: if exchangeID = 1 then dim myPlaceBets() as As UK.exchange.PlaceBets many statements myPlaceBetsReq.bets = myPlaceBets else...
6
by: =?Utf-8?B?bWljaGFlbCBzb3JlbnM=?= | last post by:
Yesterday Visual Studio gave me a strange error both at compiletime and at designtime that had no obvious connection to anything I had changed recently. After some effort tracking down the problem...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.