在 .NET 中读取 XML 格式为相反十进制字符 (period/comma) 的数字

Reading XML with numbers formatted with opposite decimal character (period/comma) in .NET

我的 VB.NET 应用正在将第 3 方网站生成的 XML 文件导入 SQL 服务器 table。该网站(和我的电脑)使用句点字符表示小数点(例如 42.015),一切正常。但一位欧洲用户报告说,导入的数字被乘以 1000 或 10000 倍。事实证明,他的计算机正在寻找逗号小数(例如 42,015),当它看到 XML 输入时,它会将其转换为42015.00.

我正在使用 DataSet.ReadXML 和 SqlBulkCopy.WriteToServer,但我不确定我可以在哪里介入来告诉程序期望周期小数。我的代码如下:

    Dim ds As New DataSet
    Try
        ds.ReadXml(tempfile, 0)
    Catch ex As XmlException
        Log($"Error reading XML: {ex.Message} with {ex.Data}")
        Exit Sub
    End Try
    Dim columnMap As New Dictionary(Of String, String) From {
        {"LOTID", "InventoryID"},
        {"ITEMTYPE", "ItemType"},
        {"ITEMID", "ItemNum"},
        {"COLOR", "ColorID"},
        {"CONDITION", "Cond"},
        {"REMARKS", "LocationName"},
        {"QTY", "Qty"},
        {"DESCRIPTION", "Description"},
        {"SUBCONDITION", "Completeness"},
        {"SALE", "Discount"},
        {"STOCKROOM", "Stockroom"},
        {"BULK", "Bulk"},
        {"BUYERUSERNAME", "Reserve"},
        {"PRICE", "Price"}
    }
    Using conn = New SqlConnection(GetDBConnectionString)
        Using sbc As New SqlBulkCopy(conn)
            conn.Open()
            DoSql(conn, "TRUNCATE TABLE dbo.Online_Inventories;")
            For Each column As DataColumn In ds.Tables("ITEM").Columns
                If columnMap.ContainsKey(column.ColumnName) Then
                      sbc.ColumnMappings.Add(column.ColumnName, columnMap(column.ColumnName))
                End If
            Next
            sbc.DestinationTableName = "Online_Inventories"
            sbc.WriteToServer(ds.Tables("ITEM"))
            conn.Close()
        End Using
    End Using

导入的 XML 如下所示:

   <ITEM>
      <LOTID>217770136</LOTID>
      <DATEADDED>9/20/2020 3:02:00 PM</DATEADDED>
      <CATEGORY>771</CATEGORY>
      <COLOR>0</COLOR>
      <PRICE>11.7563</PRICE>
      <QTY>1</QTY>
      <BULK>1</BULK>
      <IMAGE></IMAGE>
      <DESCRIPTION></DESCRIPTION>
      <CONDITION>U</CONDITION>
      <SUBCONDITION>I</SUBCONDITION>
      <ITEMTYPE>S</ITEMTYPE>
      <ITEMID>41110-1</ITEMID>
      <SALE>0</SALE>
      <REMARKS></REMARKS>
      <STOCKROOM>Y</STOCKROOM>
      <MYWEIGHT>0</MYWEIGHT>
      <ITEMWEIGHT>0</ITEMWEIGHT>
      <DATELASTSOLD></DATELASTSOLD>
      <BASECURRENCYCODE>USD</BASECURRENCYCODE>
   </ITEM>

所以在这个例子中,在第三行(ds.ReadXml)之后,ds("Price")="11.7563",一个字符串 在 sbc.WriteToServer 行之后,dbo.Online_Inventories.Price 的值为 117563.0(在这种情况下实际上是一个错误,因为 Price 是一个 NUMERIC(9,4))

当用户的本国文化使用逗号作为小数时,我如何让 .net 将句点读取为小数?谢谢!

默认线程 CultureInfo 基于 运行 机器的设置文化。默认字符串解析将使用默认的 CultureInfo。您可以在执行您发布的代码时更改线程 CultureInfo 以使用 InvariantCulture(基本上 en-US)。 InvariantCulture 使用句点 (.) 作为小数点。

Dim currentCulture As CultureInfo = Threading.Thread.CurrentThread.CurrentCulture
Threading.Thread.CurrentThread.CurrentCulture = CultureInfo.InvariantCulture
' ***
' insert your code here
' ***
Threading.Thread.CurrentThread.CurrentCulture = currentCulture