Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 114533
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:52:33+00:00 2026-05-11T02:52:33+00:00

I am running into quite an annoying issue while trying to deserialise a specific

  • 0

I am running into quite an annoying issue while trying to deserialise a specific XML document using XmlSerializer.Deserialize() method.

Basically, I have a strongly typed XSD with an element of type double. When trying to deserialise the element for a specific XML document, I get the usual ‘System.FormatException: Input string was not in a correct format.’ exception because in that specific document, the element does not have a value.

Here is some code for you nerds out there.

Sample XML document:

<TrackInfo>   <Name>Barcelona</Name>   <Length>4591</Length>   <AverageSpeed /> </TrackInfo> 

XSD:

<?xml version='1.0' encoding='utf-8'?> <xs:schema attributeFormDefault='unqualified' elementFormDefault='qualified' xmlns:xs='http://www.w3.org/2001/XMLSchema'> <xs:element name='TrackInfo'>   <xs:complexType>     <xs:sequence>       <xs:element name='Name' type='xs:string' />       <xs:element name='Length' type='xs:double' default='0.0' />       <xs:element name='AverageSpeed' type='xs:double' default='0.0' />     </xs:sequence>   </xs:complexType> </xs:element> 

TrackInfo class:

[Serializable] public class TrackInfo {   private string name = string.Empty;   private double length = 0.0;   private double averageSpeed = 0.0;    [XmlElement]   public string Name   { ... }    [XmlElement]   public double Length   { ... }    [XmlElement]   public double AverageSpeed   { ... } } 

Sample deserialisation code:

XmlDocument xmlDocument = new XmlDocument(); xmlDocument.Load('TrackInfo.xml');  // Deserialise XML string into TrackInfo object byte[] buffer = ASCIIEncoding.UTF8.GetBytes(xmlDocument.InnerXml); MemoryStream stream = new MemoryStream(buffer); System.Xml.XmlReader reader = new System.Xml.XmlTextReader(stream);  XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(TrackInfo)); TrackInfo trackInfo = (TrackInfo)xSerializer.Deserialize(reader); 

I know that the deserialisation exception comes from the fact that an empty string cannot be converted to a double. I also know that the default value is not assigned to AverageSpeed because, effectively, an empty string is a perfectly acceptable value.

Is there an easy way to default double values to 0.0 (or any other type) when deserialising if an empty string value is found in the XML document? Ideally, I would like to avoid implementing ISerializable because I don’t really feel like spending the rest of the day into the burning pit of hell (i.e. implementing ISerializable for about a hundred classes).

Cheers! Jean-Michel

  • 1 1 Answer
  • 1 View
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:52:33+00:00Added an answer on May 11, 2026 at 2:52 am

    You can specify the default value like

        [XmlElement]     [System.ComponentModel.DefaultValueAttribute(0.0)]     public double AverageSpeed     { ... } 

    /edit: ok, strange beaviour here. Whatever I set as value in the Attribute it’s always the fields value:

    private double averageSpeed = 2.0; 

    But no exception occurs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm running into an issue where I have a FileUpload control in an UpdatePanel.
I'm running into a strange issue.... This all worked fine last night when I
I'm designing my first page using CSS3, and I'm running into a snag. I
Running into a problem where on certain servers we get an error that the
Been running into this problem lately... When debugging an app in VS.Net 2005, breakpoints
I am currently running into a problem where an element is coming back from
I'm running into a mental roadblock here and I'm hoping that I'm missing something
We're running into issues with how we specify font sizes. If we specify the
I'm running into a problem setting up my development environment. I've been working on
I've been running into this problem with Flex for nearly a year, and each

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.