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