1

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!

Kamil Zemczak
  • 75
  • 1
  • 6
  • Maybe this? https://stackoverflow.com/a/9160150/5351370 – Biesi Jul 11 '17 at 09:22
  • Sure, but when i have to call this code? Before every call GetXml() method or somewhere in startUp class? – Kamil Zemczak Jul 11 '17 at 09:29
  • 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 Jul 11 '17 at 09:33
  • "The problem is, when I want to parse this XML back to myDataSet" - how are you doing that? Please provide a [mcve]. – Jon Skeet Jul 11 '17 at 09:40
  • 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 Jul 11 '17 at 09:42
  • @JonSkeet This is how I parse XML to myDataSet, exactly to DataTable http://static.pokazywarka.pl/a/4/p/524c61643906d37e48ae1eea2552ca5b_big.jpg?1499766672 – Kamil Zemczak Jul 11 '17 at 09:51
  • No, we don't want a link to a screenshot - please put a [mcve] *in the question*. – Jon Skeet Jul 11 '17 at 09:58
  • @JonSkeet I've edited question – Kamil Zemczak Jul 11 '17 at 10:20
  • But you still haven't provided a [mcve]. We can't copy, paste, compile and run. (It's not clear why you're not calling `DataSet.ReadXml` or `DataTable.ReadXml` to reverse the operation...) – Jon Skeet Jul 11 '17 at 10:40
  • Why the insistence on an MCVE? It's clear what's going on. DataSet.GetXml() will of course export decimal values with a period separator. Attempting to add a string value to a decimal column will result in a conversion using the Locale of the DataTable. – Joe Jul 11 '17 at 10:59

2 Answers2

0

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

Test12345
  • 1,625
  • 1
  • 12
  • 21
0

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.

Joe
  • 122,218
  • 32
  • 205
  • 338