473,386 Members | 1,673 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.

XMLSerializer, customize float serialization

Hello,
I am using the XmlSerializer clas to serialize/deserialize XML files.
I have a XML file containing elements which have attributes of type
float. If the value of the float attribute in my application is for
example 43.5678, after the serialization I get the value
"43.5677999999999". Is it any way to control the formatting of the float
type, to limit for example the number of positions after the comma ?
The only solution that I found is to change the attribute type from
float to string, and format the string programmatically by myself ... Is
there any better solution available ? I did not found any method to
overload the "ToString()" method for the floats ... And also the
XmlWriterSettings class does not offer any interface to control the
float format ...
Thanks in advance for your help.
Regards,
Abra
*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '06 #1
2 5125
Abra,

The string method isn't a bad solution. Just make sure you either
TryParse (preferrable) or catch NumberFormatException (less preferred)
on deserialization.

Part of the problem is that you don't really want a float. The datum
you're trying to convey in your example is better cast into a decimal
type than a float type. If you were using decimal, you wouldn't have
this problem.
Stephan

Abra wrote:
Hello,
I am using the XmlSerializer clas to serialize/deserialize XML files.
I have a XML file containing elements which have attributes of type
float. If the value of the float attribute in my application is for
example 43.5678, after the serialization I get the value
"43.5677999999999". Is it any way to control the formatting of the float
type, to limit for example the number of positions after the comma ?
The only solution that I found is to change the attribute type from
float to string, and format the string programmatically by myself ... Is
there any better solution available ? I did not found any method to
overload the "ToString()" method for the floats ... And also the
XmlWriterSettings class does not offer any interface to control the
float format ...
Thanks in advance for your help.
Regards,
Abra
*** Sent via Developersdex http://www.developersdex.com ***
Nov 13 '06 #2
Read my post at
http://msdn.microsoft.com/newsgroups...99f&sloc=en-us
for information on how to implement custom serialization.
--
Good luck!

Shailen Sukul
Architect
(BSc MCTS, MCSD.Net MCSD MCAD)
Ashlen Consulting Service P/L
(http://www.ashlen.net.au)
"Abra" wrote:
Hello,
I am using the XmlSerializer clas to serialize/deserialize XML files.
I have a XML file containing elements which have attributes of type
float. If the value of the float attribute in my application is for
example 43.5678, after the serialization I get the value
"43.5677999999999". Is it any way to control the formatting of the float
type, to limit for example the number of positions after the comma ?
The only solution that I found is to change the attribute type from
float to string, and format the string programmatically by myself ... Is
there any better solution available ? I did not found any method to
overload the "ToString()" method for the floats ... And also the
XmlWriterSettings class does not offer any interface to control the
float format ...
Thanks in advance for your help.
Regards,
Abra
*** Sent via Developersdex http://www.developersdex.com ***
Nov 14 '06 #3

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

Similar topics

0
by: keith bannister via .NET 247 | last post by:
(Type your message here) -------------------------------- From: keith bannister Hi, I'm new to .net (as of last week) but here goes. I want to serialize/deserialize a file the conforms...
5
by: Keith Bannister | last post by:
I'm new to .net so here goes. I'm tying to deserialize a class that is associated with an XML schema. I created the C# class with xsd.exe as below: xsd.exe /c /n:somenamespace...
1
by: Xing Zhou | last post by:
It seems that XmlSerializer.FromTypes() method cannot handle inner class properly .. or is there anything I did wrong? The following code will throw a reflection exception at FromTypes() method....
3
by: Don McNamara | last post by:
Hi, I've hit quite a strange problem with XmlSerializer on my W2K3 server. When I serialize/deserialize using an exe on my local computer (XP), everything works fine. When I put the code out on...
0
by: William Stacey [MVP] | last post by:
Had a method that got some string info from mp3 tags in N files and serializes this class and deserializes at other side. Works ok except sometimes get chars that choke the XmlSerializer. After...
10
by: Henrik Dahl | last post by:
Hello! I have an xml schema which has a date typed attribute. I have used xsd.exe to create a class library for XmlSerializer. The result of XmlSerializer.Serialize(...) should be passed as the...
1
by: =?Utf-8?B?Q2hyaXN0aWFu?= | last post by:
hello everyone, I'm using the XmlSerializer to generate Xml-Files of my classes. I do have some float's and double's that also should get serialized. At the moment, XmlSerializer generates...
0
by: harshal.borade | last post by:
I am using XmlSerializer with XmlAttributeOverrides. The XmlAttributeOverrides are used for properties which hold element/s derived from "AbstractMessage" type. ...
1
by: =?iso-8859-2?Q?S=B3awomir_Krzy=BFanowski?= | last post by:
When I execute this code everything works fine, no exception is throwed. Method GetObjectData was not entered, but object was serialized and file cos.xml was created with correct data. When I use...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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...

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.