Problem in calculation with different region (point and comma)

Hobbyist_programmer 621 Reputation points
2021-04-26T14:55:54.93+00:00

Hallo,

I have lot of xml tables with decimal numbers and they are formatted according to english or with dot (.) . I have an another system which has German system or it works with comma ( , ). My program converts all my xml table to List of objects when it loads, When i open my program in German system the converted list does not contain any decimals because in german system it takes . as comma.

how do i prevent my xmls being translated to list with local settings or what is the right method to approach or solve the problem?

Thanks

Developer technologies | VB
{count} votes

Accepted answer
  1. Viorel 122.6K Reputation points
    2021-04-26T18:19:24.407+00:00

    Try adding this line before the code that converts the data incorrectly:

    CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
    

    If it does not work, show more details about your code that converts the data.


2 additional answers

Sort by: Most helpful
  1. Yitzhak Khabinsky 26,586 Reputation points
    2021-04-26T15:55:39.82+00:00

    HI @Hobbyist_programmer ,

    In the XML-Schema spec a double/decimal data type needs to be represented with a dot.

    Please check here for a possible solution: xml-deserialization-of-double-value-with-german-decimal-separator-in-c-sharp


  2. Hobbyist_programmer 621 Reputation points
    2021-04-26T18:40:35.033+00:00

    Thanks Viorel, It worked .. but below is my code i use to convert my datatable to List of Objects.

        Public ntI As Type = Type.GetType("System.Nullable`1[System.Int32]")
        Public ntD As Type = Type.GetType("System.Nullable`1[System.Double]")    
    
    Private Function ConvertDataTable(Of T As New)(table As DataTable) As List(Of T)
            Return New List(Of T)(table.AsEnumerable().Select(Function(row) GetItem(Of T)(row)))
        End Function
    
        Public Function GetItem(Of T As New)(row As DataRow) As T
            Dim item As New T
            Dim itemType = GetType(T)
            For Each column As DataColumn In row.Table.Columns
                Dim prop = itemType.GetProperty(column.ColumnName)
                If prop IsNot Nothing Then
                    If prop.PropertyType.Equals(ntI) Then
                        prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntI.GetGenericArguments()(0))))
                    ElseIf prop.PropertyType.Equals(ntD) Then
                        CultureInfo.CurrentCulture = CultureInfo.GetCultureInfo("en-US")
                        prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), ntD.GetGenericArguments()(0))))
                    Else
                        prop.SetValue(item, If(row.IsNull(column), Nothing, Convert.ChangeType(row(column), prop.PropertyType)))
                    End If
                End If
            Next
    
            Return item
    
    
        End Function
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.