XmlConvert.ToDouble(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
public:
static double ToDouble(System::String ^ s);
public static double ToDouble (string s);
static member ToDouble : string -> double
Public Shared Function ToDouble (s As String) As Double
参数
- s
- String
要转换的字符串。
返回
与该字符串等效的 Double。
例外
s
为 null
。
s
的格式不正确。
s
表示小于 Double.MinValue 或大于 Double.MaxValue 的数字。
示例
以下示例使用 ToDouble
和 ToDateTime 读取强类型数据。
#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();
}
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();
}
}
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
该示例使用 文件 orderData.xml
作为输入。
<order date="2001-05-03">
<orderID>367A54</orderID>
<custID>32632</custID>
<price>19.95</price>
</order>
注解
如果 s
为 INF 或 -INF,则此方法分别返回 Double.PositiveInfinity 或 Double.NegativeInfinity。