473,699 Members | 2,458 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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
XmlSerializatio n without any problems. I've also read the documentation on
the XML attribute classes such as XmlElement and XmlRoot etc as well as
XmlAttributeOve rrides.

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
XMLConvertAttri bute 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 IXMLSerializabl e 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 1760
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 MyFieldSpecifie d;

private int _MyField;
public int MyField
{
get
{
return _MyField;
}
set
{
if( value == 0 )
{
MyFieldSpecifie d = false;
}
else
{
MyFieldSpecifie d = 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
IXmlSerialziabl e. 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******** ********@TK2MSF TNGP12.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
XmlSerializatio n without any problems. I've also read the documentation on
the XML attribute classes such as XmlElement and XmlRoot etc as well as
XmlAttributeOve rrides.

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
XMLConvertAttri bute 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 IXMLSerializabl e 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
25411
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
11
12773
by: Aaron Queenan | last post by:
Given the classes: class Class { public static implicit operator int(Class c) { return 0; } } class Holder
6
1442
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 Property MyID() As SInteger Get
5
2549
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
2989
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 sizeof(T). (Proof: In `T array;' both `array' and `array' must be correctly aligned.) Since `sizeof(unsigned char)' is divisible only by one and since one is a divisor of every type's size, every type is suitably aligned for `unsigned char'."
9
5049
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 Set(ByVal Value As Image)
1
2154
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 in Test Container. Since i attached this TypeConverter to my property, ALL custom properties (properties implemented by myself) are not displayed in Test Container.
2
1669
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 dim myPlaceBets() As AU.exchange.PlaceBets many statements
6
3338
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 I discovered first a workaround, then the real cause of the problem. I would like to understand why what I am doing is frowned upon by Visual Studio and how to do this properly. My application is in one solution; supporting libraries including...
0
8686
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8911
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8882
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5872
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4627
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2009
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.