FIX: The DataSet.WriteXML method outputs a decimal value as scientific notation and causes a System.FormatException exception in subsequent DataSet.ReadXML method calls

Article translations Article translations
Article ID: 321542 - View products that this article applies to.
This article was previously published under Q321542
Expand all | Collapse all

On This Page

SYMPTOMS

When you use an XML document that contains an attribute or an element with a decimal value such as 0.00001, the schema marks that attribute or element as a decimal. If you use the DataSet.ReadXML method to read an XML file, and then you use the DataSet.WriteXML method to write the value back to the XML file, the output contains a value in scientific notation. If you use the DataSet.ReadXML method again, you receive the following exception error message:
System.FormatException - Input string was not in a correct format.

CAUSE

The DataSet.WriteXML method internally calls the Decimal.ToString method when it writes decimal numbers. The Decimal.ToString method writes decimal numbers such as 0.00001 in scientific notation. You cannot set the NumberStyles enumeration for the DataSet object to make for scientific notation be read as a decimal value. Therefore, when you use the DataSet.ReadXML method to read the number back into a data set, a FormatException exception occurs.

RESOLUTION

A supported hotfix is now available from Microsoft, but it is only intended to correct the problem that is described in this article. Only apply it to systems that are experiencing this specific problem. This hotfix may receive additional testing. Therefore, if you are not severely affected by this problem, we recommend that you wait for the next service pack that contains this hotfix.

To resolve this problem immediately, contact Microsoft Product Support Services to obtain the hotfix. For a complete list of Microsoft Product Support Services telephone numbers and information about support costs, visit the following Microsoft Web site:
http://support.microsoft.com/contactus/?ws=support
Note In special cases, charges that are ordinarily incurred for support calls may be canceled if a Microsoft Support Professional determines that a specific update will resolve your problem. The usual support costs will apply to additional support questions and issues that do not qualify for the specific update in question. The English version of this hotfix has the file attributes (or later file attributes) that are listed in the following table. The dates and times for these files are listed in coordinated universal time (UTC). When you view the file information, it is converted to local time. To find the difference between UTC and local time, use the Time Zone tab in the Date and Time tool in Control Panel.
   Date         Time   Version       Size       File name
   -----------------------------------------------------------
   03-May-2002  02:17  1.0.3705.271  1,294,336  System.xml.dll   

WORKAROUND

You can mark the value as a string instead of as a decimal in the schema, and you can manually convert the string to a decimal number value by using the Decimal.Parse method with the AllowExponent NumberStyles flag.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

Steps to reproduce the behavior

This method uses a DataSet object to read, to write, and to read a decimal value from an XML document:
  1. Create a new Microsoft Visual C# .NET console application.
    1. Start Microsoft Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. Under Project Types, click Visual C# Projects, and then click Console Application under Templates. By default, a class that is named Class1 is created.
  2. Add the following using statement at the beginning of the Class1.cs file:
    using System.Data;
    using System.Xml; 
    
  3. Add the following code to the Main field in Class1.cs:
    DataSet ds = new DataSet();
    ds.ReadXmlSchema("..\\..\\XMLSchema1.xsd");
    ds.ReadXml("..\\..\\XMLFile1.xml");
    ds.WriteXml("..\\..\\XMLFile1.xml");		
    ds.ReadXml("..\\..\\XMLFile1.xml");
    
  4. In Solution Explorer, right-click the project name, point to Add, and then click Add New Item. The Add New Item box appears.
  5. Click XML Schema under Templates, and then click Open. By default, XMLSchema1 is created.
  6. Switch to the XML field, and then replace the coder with the following code:
    <?xml version="1.0" standalone="yes"?>
    <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
      <xs:element name="NewDataSet" msdata:IsDataSet="true">
        <xs:complexType>
          <xs:choice maxOccurs="unbounded">
            <xs:element name="Table">
              <xs:complexType>
                <xs:sequence>
                  <xs:element name="name" type="xs:string" minOccurs="0" />
                  <xs:element name="tax" type="xs:decimal" minOccurs="0" />
                </xs:sequence>
              </xs:complexType>
            </xs:element>
          </xs:choice>
        </xs:complexType>
      </xs:element>
    </xs:schema>
    
  7. Repeat step 4, click XML File under Templates, and then click Open. By default, XMLFile1 is created.
  8. Replace the code with the following code:
    <?xml version="1.0" standalone="yes" ?>
    <diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    	<NewDataSet>
    		<Table diffgr:id="Table1" msdata:rowOrder="0">
    			<name>test </name>
    			<tax>0.00001</tax>
    		</Table>
    		<Table diffgr:id="Table2" msdata:rowOrder="1">
    			<name>234</name>
    			<tax>12</tax>
    		</Table>
    	</NewDataSet>
    </diffgr:diffgram>
    
  9. On the Debug menu, click Start.
You receive the error message that is mentioned in the "Symptoms" section.

Properties

Article ID: 321542 - Last Review: October 11, 2005 - Revision: 1.6
APPLIES TO
  • Microsoft Visual Studio .NET 2002 Professional Edition
  • Microsoft Visual Studio .NET 2002 Enterprise Architect
  • Microsoft Visual Studio .NET 2002 Enterprise Developer
  • Microsoft Visual Studio .NET 2002 Academic Edition
  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.0 Service Pack 1
  • Microsoft .NET Framework 1.0 Service Pack 2
Keywords: 
kbhotfixserver kbqfe kbvs2002sp1sweep kbprogramming kbschema kbxml kbqfe kbbug kbfix KB321542

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com