On converting strings to numeric in French (or Dutch for that matter)
I received some feedback on the Sharepoint extension article that I would like to share (I had to change < to [ in the code to get CommunityServer to display it):
Hi,
Thanks for sharing this great code. I encountered 2 problems and could find some workaround:
1) Columns with a number data type would throw an exception on a French Machine due to the impossibility to convert xml number automatically because of the decimal sign (which is "," in french). I guess it may also be the case in other languages. I used this workaround :
if ( col.DataType == typeof(Single) )
newRow[col] = XmlConvert.ToSingle(val);
else
newRow[col] = val;
2) GetListItems return data base on the default view if no view is specified. If the default view has some filter, you will collect a filtered list of items. I used this workaround:
XmlElement query = ListInfoNode.OwnerDocument.CreateElement("Query");
query.InnerXml ="[WHERE>[GT>[FIELDREF Name='\"ID\"' />[VALUE Type='\"Counter\"'>0[/VALUE>[/GT>[/WHERE>";
Frédéric LATOUR
About the second remark: that is a great trick, Frédéric! Thanks for sharing.
Now about the converting of numerics in non-english cultures. I think that the XML returned by the Sharepoint web service is not using English or American formatting, but rather the InvariantCulture. Now rather than making different conversions for different types (have you considered Double, DateTime, etc...), I would prefer to let the framework do the figuring. I think that everything will work out fine if you set the CurrentCulture of the thread to CultureInfo.InvariantCulture while copying data from the XML to the DataRow. I haven't had the time to test yet, but give it a try:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
Don't forget to set your culture back to it's original setting.
POSTSCRIPT:
I have tested this situation now and it seems indeed to be solved by setting the current culture to InvariantCulture. Before the foreach loop (the one that copies fields from XML to the datatable), you insert this code:
System.Globalization.CultureInfo oldCulture = System.Threading.Thread.CurrentThread.CurrentCulture;
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
and after the loop, you set you preferred culture back:
System.Threading.Thread.CurrentThread.CurrentCulture = oldCulture;
Ready!