Click here to Skip to main content
15,741,692 members
Articles / Programming Languages / C#
Posted 7 May 2010


10 bookmarked

Pass Dynamic List of Parameters to Web Service

Rate me:
Please Sign up or sign in to vote.
4.81/5 (9 votes)
21 May 2010CPOL1 min read
Make interfacing with your web service simpler and more maintainable
I'm working on a Silverlight 3 project, and I have to connect to a database through a web service. One of the aspects of this web service is that there is a single common method that accepts the name of the stored proc to execute, and one or more parameters of various types.

Some things I learned along the way to figuring out my not-so-elegant solution:

0) Silverlight doesn't support the Serialize attribute.

1) A web service method can't accept generic lists of objects (nor apparently an array of object objects).

After playing with this for a couple of days, I came up with the following solution. The short version is that I created a xml string in the client, and parse it in the web service. To convert to a xml string, I did this:

public static XElement MakeXmlParameter(string name, object value)
    XElement element = new XElement("Parameter",
                                     new XElement("Name", name),
                                     new XElement("Type", value.GetType()),
                                     new XElement("Value", value));
    return element;

And in the service, I have this method:

public class Parameters :List<SqlParameter>

private Parameters MakeSqlParameters(string xmlData)
    Parameters parameters = null;
    if (string.IsNullOrEmpty(xmlData))
	XDocument  xDoc       = XDocument.Parse(xmlData);
	XElement   root       = xDoc.Element("Parameters");
	if (root.HasElements)
            parameters = new Parameters();
	    IEnumerable<xelement> children = (from item in root.Elements() select item);
	    foreach (XElement value in children)
		string paramName       = value.Element("Name").Value;
		string typeName        = value.Element("Type").Value;
		object obj             = Convert.ChangeType(value.Element("Value").Value, 
		SqlParameter parameter = new SqlParameter(paramName, obj);
    return parameters;

There's probably a better way to do this given the project architecture, but I needed to get something working fast.

Side benefit - notice the call to the Convert.ChangeType method, I searched google for several hours before finding out that you could convert to a named type this way. Look at how much time I saved you from having to do the same google search! :)


You can make the MakeSqlParameters() method more generic by simply retrieving the first element before looping through all of the child elements. That way, it doesn't matter what they called the root node.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Written By
Software Developer (Senior) Paddedwall Software
United States United States
I've been paid as a programmer since 1982 with experience in Pascal, and C++ (both self-taught), and began writing Windows programs in 1991 using Visual C++ and MFC. In the 2nd half of 2007, I started writing C# Windows Forms and ASP.Net applications, and have since done WPF, Silverlight, WCF, web services, and Windows services.

My weakest point is that my moments of clarity are too brief to hold a meaningful conversation that requires more than 30 seconds to complete. Thankfully, grunts of agreement are all that is required to conduct most discussions without committing to any particular belief system.

Comments and Discussions

Questionweb service Pin
Member 1105298115-Sep-14 1:45
Member 1105298115-Sep-14 1:45 
GeneralSome cool observations. Thanks. Pin
Abhinav S9-May-10 0:33
Abhinav S9-May-10 0:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.