简体   繁体   English

在 .NET 中使用以相反十进制字符(句点/逗号)格式化的数字读取 XML

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

My VB.NET app is importing a XML file generated by a 3rd-party website into a SQL Server table.我的 VB.NET 应用程序将第 3 方网站生成的 XML 文件导入到 SQL Server 表中。 The website (and my computer) use the period character for decimals (eg 42.015) and everything works great.该网站(和我的计算机)使用句点字符表示小数(例如 42.015),并且一切正常。 But a European user reported that numbers imported were being multiplied by a factor of 1000 or 10000. It turns out that his computer is looking for a comma decimal (eg 42,015) and when it sees the XML input it converts it to 42015.00.但是一位欧洲用户报告说,导入的数字乘以 1000 或 10000 的因数。结果他的计算机正在寻找逗号小数(例如 42,015),当它看到 XML 输入时,它会将其转换为 42015.00。

I'm using DataSet.ReadXML and SqlBulkCopy.WriteToServer and I'm not sure where I can step in to tell the program to expect period decimals.我正在使用 DataSet.ReadXML 和 SqlBulkCopy.WriteToServer 并且我不确定我可以在哪里介入告诉程序期望周期小数。 My code is below:我的代码如下:

    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

The XML imported looks like this:导入的 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>

So in this example, after the third line (ds.ReadXml), ds("Price")="11.7563", a string After the line sbc.WriteToServer, the value of dbo.Online_Inventories.Price is 117563.0 (actually an error in this case because Price is a NUMERIC(9,4))所以在这个例子中,在第三行(ds.ReadXml)之后,ds("Price")="11.7563",一个字符串 sbc.WriteToServer 行之后,dbo.Online_Inventories.Price 的值是117563.0(实际上是一个错误这种情况下,因为价格是一个 NUMERIC(9,4))

How do I get .net to read periods as decimals when the user's home culture uses commas as decimals?当用户的家庭文化使用逗号作为小数时,如何让 .net 将句点读取为小数? Thanks!谢谢!

The default thread CultureInfo is based on the running machine's set culture.默认线程 CultureInfo 基于运行机器的设置文化。 Default string parsing will use the default CultureInfo.默认字符串解析将使用默认 CultureInfo。 You can change the thread CultureInfo to use the InvariantCulture (basically en-US) while executing the code you posted.您可以在执行您发布的代码时更改线程 CultureInfo 以使用 InvariantCulture(基本上是美国)。 The InvariantCulture uses a period(.) for the decimal mark. InvariantCulture 使用句点 (.) 作为小数点。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM