问题描述
我有一个项目,该项目从 xml 文件
Convert.ToDouble方法读取一些数据。
会将0.05转换为5。
有解决这个问题的主意吗?
I have a project that reads some data from xml fileConvert.ToDouble metod works wrong.it converts 0.05 as 5.is there any idea to solve this problem?
这是我的ReadDataFromXml方法:
this is my ReadDataFromXml method:
public static List<double> XmldenTabanDegerleriniOku(string ID)
{
string ayarDir = System.Environment.CurrentDirectory + "\\Ayarlar";
string Dosya = ayarDir + "\\YuzeyBoyutlari.xml";
XmlDocument doc = new XmlDocument();
doc.Load(Dosya);
XmlNodeList holList = doc.GetElementsByTagName("HOL");
List<double> tabanDegerleri = new List<double>();
foreach (XmlNode node in holList)
{
XmlElement holElement = (XmlElement)node;
if (node.Attributes["ID"].Value == ID) {
double uzunluk =Convert.ToDouble(holElement.GetElementsByTagName("uzunluk")[0].InnerText.Replace('.',','));
double genislik =Convert.ToDouble(holElement.GetElementsByTagName("genislik")[0].InnerText.Replace('.',','));
double cizgilerArasiMesafe = Convert.ToDouble(holElement.GetElementsByTagName("cizgilerArasiMesafe")[0].InnerText.Replace('.', ','));
tabanDegerleri.Add(uzunluk);
tabanDegerleri.Add(genislik);
tabanDegerleri.Add(cizgilerArasiMesafe);
break;
}
}
return tabanDegerleri;
}
推荐答案
不要使用 Convert.ToDouble
然后:使用。我认为可以有效地使用不变文化(因为在这种意义上,XML文件传输数据不应该是特定于文化的。)
Don't use Convert.ToDouble
then: use XmlConvert.ToDouble
. I believe that effectively uses the invariant culture (as XML documents conveying data shouldn't be culture-specific in that sense).
编辑:我没注意到你是手动将。替换为,-因此,当您说将0.05转换为5时,您的意思是将0.05转换为5。您应该使用 XmlConvert
停止自己弄乱数据。
I hadn't noticed that you were manually replacing '.' with ',' - so when you say that "it is converting 0.05 as 5" you really mean "it is converting 0,05 as 5". You should use XmlConvert
and stop messing with the data yourself.
这篇关于xml Convert.ToDouble工作错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!