In order to pass a varying number of parameters from a web application to a database stored procedure, I thought using an XML parameter would be a good solution. The problem is, the data in question is stored in a C# hash table. I'd like to convert that hash table into an XML variable which would then be passed to the database stored procedure. I've done some research but I'm unable to find a solution.
Asked
Active
Viewed 651 times
0
-
You're using the non-generic [`Hashtable`](https://msdn.microsoft.com/en-us/library/system.collections.hashtable.aspx)? 1) Can you switch to a typed generic dictionary instead? I strongly recommend doing so as it will make testing and serialization easier. 2) Do you know at compile time what will be inside the hash table? `XmlSerializer` needs to be informed of types to expect at compile time. 3) What does your hash table contain, and what XML do you want to see from it? – dbc Jun 02 '16 at 18:41
-
Here's a serializable dictionary by the way: [XML Serializable Generic Dictionary](http://weblogs.asp.net/pwelter34/444961). See also [Serialize Class containing Dictionary member](https://stackoverflow.com/questions/495647) and [Why doesn't XmlSerializer support Dictionary?](https://stackoverflow.com/questions/2911514). – dbc Jun 02 '16 at 18:44
-
Yes, I'm using the System.Collections.HashTable. 1) I'm working with a RadGrid control, interrogating the updated values. RadGrid provides a function called ExtractValuesFromItem which returns an IDictionary. 2) No, at compile time you can't tell what will be in the HashTable. The users can pick from a dynamic list of sources 3) The hash table will contain column names (keys) and their associated values. I didn't realize XmlSerializer needs to know types. – Matthew Walk Jun 02 '16 at 18:47
-
It does, see for instance [Declaring Serialization Types](https://msdn.microsoft.com/en-us/library/aa302290.aspx#trblshtxsd_topic4) or [type xxxx not expected use xmlinclude or soapinclude](https://stackoverflow.com/questions/20003585). – dbc Jun 02 '16 at 18:58
-
Is there any chance you could reconsider your basic design and switch from XML to JSON? [tag:json.net] is much more willing to serialize and deserialize dynamic types. For instance, if you set [`TypeNameHandling = TypeNameHandling.All`](http://www.newtonsoft.com/json/help/html/SerializeTypeNameHandling.htm) you could serialize your `Hashtable` as a `List
` and the JSON will contain the necessary type information. – dbc Jun 02 '16 at 19:29 -
The goal is to get the data into a SQL Server database. Originally, I was going to use a Table Value Parameter, but since the list of columns is variable, I thought it would be best to use an XML parameter. – Matthew Walk Jun 02 '16 at 21:00