Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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;
           cmd.Parameters.Add(dataParameter);

           try
           {
               conn.Open();
               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)
           {
               throw;
           }
           finally
           {
               reader.Close();
               conn.Close();
           }
       }
Posted
Updated 4-Dec-12 8:34am
v2

1 solution

Try this

With Generics
XML
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
C#
function T Deserialize<T>(string s)
{
    var serializer = new XmlSerializer(typeof(T));
    var stringReader = new StringReader(serializedResults);

    var obj = (T)serializer.Deserialize(stringReader);
    stringReader.Dispose();

    return obj;
}
 
Share this answer
 
v2
Comments
Richard C Bishop 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.
Richard C Bishop 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900