如何从我的Web服务在ASP页面上以XML格式显示返回数据? [英] How do I display the return data in XML on my ASP page from my web service?

查看:96
本文介绍了如何从我的Web服务在ASP页面上以XML格式显示返回数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个简单的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.

推荐答案

我发现了我的问题。我没有在我的VBScript中创建DOM文档来保存我的xml响应。此外,我从存储过程中序列化我的数据也需要一些调整。



这是我添加的内容:



这是我的VBScript。

I figured out my problem. I was not creating a DOM Document in my VBScript to hold my xml response. Also, my serializing of my data from the stored procedure needed some tweaking too.

Here is what I added:

This is my VBScript.
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);


你最好编写在IE以外的浏览器中工作的网页。然后,您还可以使用Chrome来调试代码。您还可以将MsgBox添加到脚本中以查看函数调用的返回,以查看返回的内容。
You would do better to write web pages that work in browsers other than IE. You could also then use Chrome to debug your code. You can also add MsgBoxes to your script to view the return from your function call, to see what is coming back.


这篇关于如何从我的Web服务在ASP页面上以XML格式显示返回数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆