473,804 Members | 3,247 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.56779999999 99". 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 programmaticall y 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
XmlWriterSettin gs 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 5231
Abra,

The string method isn't a bad solution. Just make sure you either
TryParse (preferrable) or catch NumberFormatExc eption (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.56779999999 99". 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 programmaticall y 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
XmlWriterSettin gs 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.56779999999 99". 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 programmaticall y 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
XmlWriterSettin gs 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
3030
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 to an XML schema (xsd).
5
7288
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 properties.xsd this creates properties.cs
1
3105
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. Is there a work-around? using System; using System.Xml; using System.Xml.Serialization;
3
3172
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 the server (W2K3) it throws an exception. It only seems to happen when serializing/deserializing _arrays_ of a type. If I just serialize/deserialize one instance, it works fine. The exception I get is: (sorry for the word wrapping.)...
0
2310
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 some digging, I found XmlSerializer chokes on 0x03 chars. It probably chokes on many others, but this one I found. It serializes ok, but chokes on deserialize on "<Field1>&#x3;</Field1>". So the questions are: 1) Why does serializer produce...
10
4789
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 value for the parameter of an SqlCommand for inserting the xml document in a column of a table where the column is typed to be of the same xml schema. This all sounds simple, but SQL Server REQUIRES the timezone to be specified for date values....
1
7780
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 something like the following for float numbers: <myVal>4000.5</myVal> But i need to get this in german culture... there shoud be a comma instead of the point, so it should look like:
0
4105
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. System.Xml.Serialization.XmlRootAttribute(Namespace = "http:// www.TestNameSpace.org/Test-4-4", IsNullable = false)] class Root { public virtual Abstract_message Message
1
406
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 BinaryFormater then Exception(" That metod... is throwed. I don't know why GetObjectData is not executed with XmlSerializer. FileStream stream = new FileStream(@"d:\cos.xml", FileMode.Open, FileAccess.ReadWrite); XmlSerializerFactory...
0
10589
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10340
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10327
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
9161
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...
1
7625
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6857
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
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4302
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
2
3828
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.