简体   繁体   English

C#如何将DataSet.GetXml()十进制分隔符更改为“,”

[英]C# How to change DataSet.GetXml() decimal separator to “,”

My default decimal separator is "," when calling method myDataSet.GetXml() all decimal values from myDataSet are saving to XML with dot "." 当调用方法myDataSet.GetXml()时,我的默认小数点分隔符为“,”,myDataSet中的所有十进制值均以点“。”保存到XML。 separator. 分隔器。 The problem is, when I want to parse this XML back to myDataSet, and VS throws me Exception that decimal field accepts only decimal values, because of this separator. 问题是,当我想将此XML解析回myDataSet时,VS抛出异常,由于该分隔符,十进制字段仅接受十进制值。

Example how i get XML: 我如何获取XML的示例:

var xml = myDataSet.GetXml(); //Gives me XML with dots in decimals

Example how i try parse to DataTable: 我如何尝试解析到DataTable的示例:

var recordsDeleted = new DataTable(); //In my code I clone table from existing
recordsDeleted.Columns.Add("decimalFirst", typeof(decimal));
recordsDeleted.Columns.Add("decimalSecond", typeof(decimal));
recordsDeleted.Columns.Add("text", typeof(string));

var paramsToDataTable = new List<string> {"12.34","22.22","Foo"}; //This comes from XML
recordsDeleted.Rows.Add(paramsToDataTable.ToArray());

Please help me, how to change separator when saving to XML, or other solution to solve problem when parsing. 请帮助我,在保存为XML时如何更改分隔符,或在解析时解决问题的其他解决方案。 Thanks! 谢谢!

您需要在应用程序开始时将CulutreInfo的数字分隔符设置为,

DataSet.GetXml() exports decimal columns with a period separator as you've noted. 如前所述, DataSet.GetXml()导出带有句点分隔符的十进制列。 You can't change this: XML schema numeric data types always use a period separator. 您不能更改此设置:XML模式数字数据类型始终使用句点分隔符。

However, this code: 但是,此代码:

recordsDeleted.Rows.Add(paramsToDataTable.ToArray());

will attempt to convert each value in paramsToDataTable.ToArray() to the appropriate data type using the culture of the DataTable . 会尝试使用DataTable的区域性将paramsToDataTable.ToArray()中的每个值转换为适当的数据类型。

This culture defaults to CultureInfo.CurrentCulture , but you can override it as follows: 该区域性默认为CultureInfo.CurrentCulture ,但是您可以按如下所示覆盖它:

var recordsDeleted = new DataTable();
recordsDeleted.Locale = CultureInfo.InvariantCulture;

You should note that it would be more usual to read XML into a DataTable using DataTable.ReadXml() . 您应该注意,使用DataTable.ReadXml()将XML读入DataTable更为常见。 If you do this, you won't need to be concerned about cultures / locales. 如果您这样做,则无需担心文化/地区。

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

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