Click to Rate and Give Feedback
MSDN
MSDN Library
.NET Development
.NET Framework 3.5
XmlConvert Class
 ToDouble Method

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
XmlConvert..::.ToDouble Method

Converts the String to a Double equivalent.

Namespace:  System.Xml
Assembly:  System.Xml (in System.Xml.dll)
Visual Basic (Declaration)
Public Shared Function ToDouble ( _
    s As String _
) As Double
Visual Basic (Usage)
Dim s As String
Dim returnValue As Double

returnValue = XmlConvert.ToDouble(s)
C#
public static double ToDouble(
    string s
)
Visual C++
public:
static double ToDouble(
    String^ s
)
JScript
public static function ToDouble(
    s : String
) : double

Parameters

s
Type: System..::.String
The string to convert.

Return Value

Type: System..::.Double
A Double equivalent of the string.
ExceptionCondition
ArgumentNullException

s is nullNothingnullptra null reference (Nothing in Visual Basic).

FormatException

s is not in the correct format.

OverflowException

s represents a number less than Double..::.MinValue or greater than Double..::.MaxValue.

If s is INF or -INF, this method returns Double.PositiveInfinity or Double.NegativeInfinity respectively.

The following example uses ToDouble and ToDateTime to read strongly typed data.

Visual Basic
Imports System
Imports System.IO
Imports System.Xml

public class Sample

  public shared sub Main()

    Dim reader as XmlTextReader = new XmlTextReader("orderData.xml")

    'Parse the file and pull out the order date and price.
    while (reader.Read())
       if (reader.NodeType=XmlNodeType.Element)
         select case reader.Name
           case "order":
             Dim orderDate as DateTime = XmlConvert.ToDateTime(reader.GetAttribute("date"))
             Console.WriteLine("order date: {0}", orderDate.ToString())
           case "price":
             Dim price as Double = XmlConvert.ToDouble(reader.ReadInnerXml())
             Console.WriteLine("price: {0}", price.ToString())
         end select
       end if
    end while

    'Close the reader.
    reader.Close()  
  end sub
end class

C#
using System;
using System.IO;
using System.Xml;

public class Sample
{

  public static void Main()
  {
    XmlTextReader reader = new XmlTextReader("orderData.xml");

    //Parse the file and pull out the order date and price.
    while (reader.Read()){
       if (reader.NodeType==XmlNodeType.Element){
         switch(reader.Name){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
             break;
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());
             break;
         }
       }
    }

    //Close the reader.
    reader.Close();  
  }
}

Visual C++
#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
int main()
{
   XmlTextReader^ reader = gcnew XmlTextReader( "orderData.xml" );

   //Parse the file and pull out the order date and price.
   while ( reader->Read() )
   {
      if ( reader->NodeType == XmlNodeType::Element )
      {
         if ( reader->Name->Equals( "order" ) )
         {
            DateTime orderDate = XmlConvert::ToDateTime( reader->GetAttribute( "date" ) );
            Console::WriteLine( "order date: {0}", orderDate.ToString() );
         }
         else
         if ( reader->Name->Equals( "price" ) )
         {
            Double price = XmlConvert::ToDouble( reader->ReadInnerXml() );
            Console::WriteLine( "price: {0}", price );
         }
      }
   }


   //Close the reader.
   reader->Close();
}


The example uses the file, orderData.xml, as input.

<order date="2001-05-03">
  <orderID>367A54</orderID>
  <custID>32632</custID>
  <price>19.95</price>
</order>

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker