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。