问题描述
我创建了一个简单的Web服务,它接受存储过程名称和一串数据。它使用这些值来创建与数据库的连接,并执行从ASP页面传递字符串值的所述存储过程。我试图将存储过程中的数据作为XML返回到ASP页面并显示它。
我会发布试图执行此操作的代码。我将不胜感激任何有关该怎么做的指导或建议,谢谢。
这是我在ASP页面上的VBScript,它从三个页面获取名称/值对输入字段并通过SOAP将信息发送到服务。
I have made a simple web service that accepts a stored procedure name and a string of data. It uses those values to create a connection to a database and executes said stored procedure that is passed the string value from the ASP page. I am trying to return the data from the stored procedure as XML back to the ASP page and display it.
I will post the code I have that is attempting to do this. I would appreciate any guidance or suggestions on what to do, thank you.
This is the VBScript I have on my ASP page that gets the name/value pairs from three input fields and sends the info via SOAP to the service.
Dim soapServer, soapMessage, data
For x = 1 To Request.Form.Count()
fieldName = Request.Form.Key(x)
fieldValue = Request.Form.Item(x)
If fieldName <> "Submit" And fieldName <> "Password" Then
data = data & fieldName & "~" & fieldValue & "|"
End If
Next
'service location - Production
soapServer = "http://intranet.domain.com:54321/DataExchangeService/DataExchangeService.svc"
'message including soap envelope wrapper, to send to the service
soapMessage = "<s:Envelope xmlns:s=" & Chr(34) & "http://schemas.xmlsoap.org/soap/envelope/" & Chr(34) & ">" & _
"<s:Body>" & _
"<DataExchange xmlns=" & Chr(34) & "http://tempuri.org/" & Chr(34) & ">" & _
"<storedProcedure>" & storedProcedure & "</storedProcedure>" & _
"<data>" & data & "</data>" & _
"</DataExchange>" & _
"</s:Body>" & _
"</s:Envelope>"
Set xmlhttp = CreateObject("MSXML2.ServerXMLHTTP")
xmlhttp.open "POST", soapServer, False
xmlhttp.setRequestHeader "Man", POST & " " & soapServer & " HTTP/1.1"
xmlhttp.setRequestHeader "SOAPAction", "http://tempuri.org/IDataExchangeService/DataExchange"
xmlhttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
'Calling the service'
xmlhttp.send(soapMessage)
Response.Write xmlhttp.responseText
这是我实际服务中的c#代码。
This is the c# code in my actual service.
public class DataExchangeService : IDataExchangeService
{
//The code below handles the customer match checking process
public string DataExchange(string storedProcedure, string data)
{
string connString = ConfigurationManager.ConnectionStrings["DataExchangeConnString"].ConnectionString;
string returnData = "";
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();
while (reader.Read())
{
returnData = SerializeToXml(reader);
}
return returnData;
}
catch (Exception)
{
throw;
}
finally
{
reader.Close();
conn.Close();
}
}//end DataExchange
public string SerializeToXml(object value)
{
StringWriter writer = new StringWriter();
XmlSerializer serializer = new XmlSerializer(value.GetType());
serializer.Serialize(writer, value);
return writer.ToString();
}
}//end dataexchangeservice
}//end namespace
没有错误,除了已经存在的HTML之外,它不会在页面上显示任何内容。
Nothing errors out, it just does not display anything on the page other than the HTML that was already there.
推荐答案
Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false
Response.Write(xmlhttp.ResponseXml.xml)
这是我的代码隐藏。
This is my code-behind.
DataTable dataTable = new DataTable();
dataTable.TableName = "ReturnData";
dataTable.Load(reader);
dataTable.WriteXml(xmlWriter, XmlWriteMode.WriteSchema, false);
这篇关于如何从我的Web服务在ASP页面上以XML格式显示返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!