xml Convert.ToDouble works wrong

依然范特西╮ 提交于 2020-04-07 00:28:44

问题


I have a project that reads some data from xml file Convert.ToDouble metod works wrong. it converts 0.05 as 5. is there any idea to solve this problem?

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;
}

回答1:


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).

EDIT: 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.




回答2:


The problem

Your are replacing . by , before converting. That means when you get 0.05 you convert it to 0,05. That conversion will behave according to your locale. In US, for instance, that is 5.

The Solution

Take a look at @JonSkeet's answer and use XmlConvert.ToDouble which is culture invariant (it uses XML standarts for data formatting). And, of course, drop the string replacements.




回答3:


You should use the correct CultureInfo instead of replacing the dots with comas.

You can do this using this signature of the Convert.ToDouble method. Something like:

double uzunluk =Convert.ToDouble(holElement.GetElementsByTagName("uzunluk")[0].InnerText, CultureInfo.CurrentCulture);

If you still have a problem with dots and comas, it means that the culture of your Xml file is not coherent with the current culture (which is the culture of the machine executing the line of code), for example your Windows installation is set to have comas as decimal separators (in this case it seems CurrentCulture is the Turkish culture, and your xml has a different one, like US Culture). In this case, you have to call Convert using the actual culture of your xml, understanding where it has been generated.

If it has dots as decimal separators, then you can try getting the common invariant culture (CultureInfo.InvariantCulture), which indeed uses dots, or maybe be more specific. (see GetCultureInfo).




回答4:


I notice you're doing some text replacement of . to ,.

Does this mean you are in a culture where there's a decimal comma rather than decimal point? In this case "0.05" is 5. If you wanted the fraction the string would have to be "0,05".




回答5:


Does the double conversion work if you convert the value before changing the decimal point to a comma? I think you have a locale issue.



来源:https://stackoverflow.com/questions/7501607/xml-convert-todouble-works-wrong

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!