This documentation is archived and is not being maintained.

XmlConvert.ToDouble Method

Converts the String to a Double equivalent.

[Visual Basic]
Public Shared Function ToDouble( _
   ByVal s As String _
) As Double
public static double ToDouble(
 string s
public: static double ToDouble(
 String* s
public static function ToDouble(
   s : String
) : double;


The string to convert.

Return Value

A Double equivalent of the string.


Exception Type Condition
ArgumentNullException s is a 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.


[Visual Basic, C#, C++] 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.
  end sub
end class

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){
           case "order":
             DateTime orderDate = XmlConvert.ToDateTime(reader.GetAttribute("date"));
             Console.WriteLine("order date: {0}", orderDate.ToString());
           case "price":
             Double price = XmlConvert.ToDouble(reader.ReadInnerXml());
             Console.WriteLine("price: {0}", price.ToString());

    //Close the reader.

#using <mscorlib.dll>
#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;

int main()
   XmlTextReader* reader = new XmlTextReader( S"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(S"order") )
            DateTime orderDate = XmlConvert::ToDateTime(reader->GetAttribute(S"date"));
            Console::WriteLine( S"order date: {0}", __box( orderDate )->ToString());
         else if ( reader->Name->Equals(S"price") )
            Double price = XmlConvert::ToDouble(reader->ReadInnerXml());
            Console::WriteLine( S"price: {0}", __box( price ));

   //Close the reader.

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

<order date="2001-05-03">

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.


Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family, .NET Compact Framework, Common Language Infrastructure (CLI) Standard

See Also

XmlConvert Class | XmlConvert Members | System.Xml Namespace | PositiveInfinity | NegativeInfinity