简体   繁体   中英

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 "." 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.

Example how i get XML:

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

Example how i try parse to 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. Thanks!

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

DataSet.GetXml() exports decimal columns with a period separator as you've noted. You can't change this: XML schema numeric data types always use a period separator.

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 .

This culture defaults to CultureInfo.CurrentCulture , but you can override it as follows:

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() . If you do this, you won't need to be concerned about cultures / locales.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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