However, if I break it down more explicitly, like this:
var temp as string = item.GetAttribute("xloc")
var tempDouble as double
tempDouble = double.FromString(temp)
r.xLoc = tempDouble
temp = "0.02" tempDouble = 0.02 r.xloc = 0.02
Which is what I want. But why is it that neither item.GetAttribute("xloc").ToDouble or double.FromString(item.GetAttribute("xloc")) seem to be directly assignable to r.xloc? Shouldn’t both of those return a double from the string provided?
I have an older version of Xojo on my laptop at home and might try to update it to the same version as I have at work later tonight so I can make a project, but it’s fairly straightforward, what I’m doing. If I can’t do it tonight, I’ll try to do it tomorrow AM when I’m back at work.
I can break things in systems whose locales don’t use period as decimal, so using Val fix it as:
// Sample XML -- Xojo 2025r2.1
Var xml_str As String = "<?xml version=""1.0"" encoding=""UTF-8""?>"+_
"<anyitem id=""123"" xloc=""0.02""></anyitem>"
Var refval As Double = 0.02
// Load
Var xmlDoc As New XMLDocument(xml_str)
// Get the root element (the 'anyitem' node in this case)
Var item As XMLNode = xmlDoc.DocumentElement
// Get the value of the 'xloc' attribute
Var xloc_str As String = item.GetAttribute("xloc")
Var xloc_dbl As Double
xloc_dbl = xloc_str.Val
//xloc_dbl = xloc_str.ToDouble //--- This return 2 in a locale where comma is the decimal marker
If xloc_dbl<>refval Then
MessageBox "Invalid value : "+xloc_dbl.ToString
Else
MessageBox "OK! It Works"
End
Quit
The documentation is clear about this:
String.ToDouble → Convert using user locale
String.Val → Convert using Standard (English) locale notation (only for decimal separator)
So I spent some more time on this and I think it’s related to ImportNode. If I directly pull a node from the XML file like this:
var item as XMLNode = XMLFile.DocumentElement.Child(0)
Then both
item.GetAttribute("xloc").ToDouble
and
double.FromString(item.GetAttribute("xloc"))
works fine.
If i use ImportNode to extract the node from the master XML file, the node is correctly imported (I can see it and all of its attributes in the debugger and when I convert it to a string for display) but the attributes are empty when I try to access them in code. So it seems to be related to issue #79744 which I reported the other day, after we discussed it here: How to insert an XML node into an existing XMLDocument