Click here to Skip to main content
13,189,324 members (66,777 online)
Rate this:
Please Sign up or sign in to vote.
See more:
I am creating a service that accepts a stored procedure and user input from an ASP page and is supposed to return the data in XML format back to the page. I am having trouble trying to figure out how to accomplish this. Can anyone suggest a better way to go about this and/or explain the flaw in my method? At first I was returning a string with the method, then I tried to just write an XML document with the results of the stored procedure in an attempt to access the XML file on the ASP page.

I have not been able to find a good example to extrapolate from. Please let me know if more clarification is needed.

Thank you!

Here is the code-behind I have:
 public void DataExchange(string storedProcedure, string data)
            string connString = ConfigurationManager.ConnectionStrings["DataExchangeConnString"].ConnectionString;
            //string returnData = "";
            int i = 0;
            SqlParameter dataParameter = new SqlParameter();
            dataParameter.Value = data;
            dataParameter.ParameterName = "@data";
            SqlConnection conn = new SqlConnection(connString);
            SqlCommand cmd = new SqlCommand(storedProcedure, conn);
            SqlDataReader reader = null;
            cmd.CommandType = CommandType.StoredProcedure;
                reader = cmd.ExecuteReader();
                string FileName = @"..\xmlDOC\returnData.xml";
                SqlDataAdapter objDataAdapter = new SqlDataAdapter(reader.ToString(), connString);
                DataSet objDataSet = new DataSet("Data");
                // Fill DataSet
                objDataAdapter.Fill(objDataSet, "Data");
                // Write DataSet contents to file
                objDataSet.WriteXml(FileName, XmlWriteMode.IgnoreSchema);
                //while (reader.Read())
                //    for (i = 0; i < reader.FieldCount; i++)
                //    {
                //        returnData += (reader[i] + "|");
                //    }
                //    return returnData;
                //return false.ToString();
            catch (Exception)
Posted 4-Dec-12 6:27am
Updated 4-Dec-12 8:34am

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

Try this

With Generics
public static string SerializeToXml<T>(T value)
    StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
    XmlSerializer serializer = new XmlSerializer(typeof(T));
    serializer.Serialize(writer, value);
    return write.ToString();

Without Generics
public static string SerializeToXml(object value)
  StringWriter writer = new StringWriter(CultureInfo.InvariantCulture);
  XmlSerializer serializer = new XmlSerializer(value.GetType());
  serializer.Serialize(writer, value);
  return writer.ToString();

To Deserialize
function T Deserialize<T>(string s)
    var serializer = new XmlSerializer(typeof(T));
    var stringReader = new StringReader(serializedResults);
    var obj = (T)serializer.Deserialize(stringReader);
    return obj;
richcb 4-Dec-12 15:23pm
So I just call that method and pass it my data that was returned from the stored procedure?
Teenustar 4-Dec-12 15:36pm
yes. I have also edited the solution for the Deserialize method... To deserialize the data back.
richcb 4-Dec-12 15:41pm
Ok, great. Now this question may be too complex but I will ask it anyway and give a bit of background. I initially sent the stored procedure name and input from my ASP page to the web service via an XML SOAP message. With the data I get back from the stored procedure now serialized after calling the method you provided, will I be able to use the xml response object to access the serialized data on the original ASP page?
Teenustar 4-Dec-12 15:50pm
Deserialize the data back to the Object and then use it in your ASP page.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web02 | 2.8.171016.2 | Last Updated 4 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100