1
votes

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!

2
Sure, but when i have to call this code? Before every call GetXml() method or somewhere in startUp class?Kamil Zemczak
Depends on where you do your work. If it's on the main thread you call it once in the beginning. If you spawn new threads for every time you call GetXml() you call it before calling GetXml() in your new thread.Biesi Grr
"The problem is, when I want to parse this XML back to myDataSet" - how are you doing that? Please provide a minimal reproducible example.Jon Skeet
I've tried to change seperator to "," by code You've sent to me one line before GetXml(), and method saved XML with dots again. I also tried to change seperator to "." just before parsing to myDataSet, and this also not worked.Kamil Zemczak

2 Answers

0
votes

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.

0
votes

You need to set the CulutreInfo With the numberseperator as , at the start of the application