【问题标题】:How to avoid HTML script when web service returnsWeb服务返回时如何避免HTML脚本
【发布时间】:2011-06-22 14:11:52
【问题描述】:
[ScriptMethod(UseHttpGet = true)]
[WebMethod]

public string sampleTest()
{
    string name = "";
    name = System.Web.HttpContext.Current.Request.QueryString["name"];

    StringBuilder sb = new StringBuilder();
    sb.Append("<message>");
    sb.Append("<categoryname =" + name + "/>");
    sb.Append("</message>");
    return sb.ToString();
}

调用方法:

StringBuilder sb = new StringBuilder();

    byte[] buf = new byte[8192];


    HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://localhost/mylytica/apibridge.asmx/sampleTest?name=Shankar");

    HttpWebResponse response = (HttpWebResponse)
        request.GetResponse();

    Stream resStream = response.GetResponseStream();

    string tempString = null;
    int count = 0;

    do
    {
        count = resStream.Read(buf, 0, buf.Length);
        if (count != 0)
        {
            tempString = Encoding.ASCII.GetString(buf, 0, count);
            sb.Append(tempString);
        }
    }

    while (count > 0);

    Literal1.Text = sb.ToString();

字面值包含:

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://tempuri.org/">&lt;message loginstatus="OK" userid="1" /&gt;</string>

实际格式:

<message loginstatus="OK" userid="1" />

我必须做什么。

【问题讨论】:

  • 我需要的实际格式是:
  • 但我得到这样的价值:&gtmessage loginstatus="OK" userid="1" /&gt

标签: xml web-services c#-2.0


【解决方案1】:
[ScriptMethod(UseHttpGet = true)]
[WebMethod]


public XmlDocument login(string username, string password)
{
       XmlDocument oXmlDoc = new XmlDocument();
       XmlNode oXmlmessage = oXmlDoc.CreateNode(XmlNodeType.Element, "message", "");
       XmlAttribute oxmllogin = oXmlDoc.CreateAttribute("loginstatus");
       oxmllogin.InnerText = "OK";

       XmlAttribute oXmluserid = oXmlDoc.CreateAttribute("userid");
       oXmluserid.InnerText = iRegID.ToString();

       oXmlmessage.Attributes.Append(oxmllogin);
       oXmlmessage.Attributes.Append(oXmluserid);

       oXmlDoc.AppendChild(oXmlmessage);

       return oXmlDoc;
}

【讨论】:

  • 最后我喜欢这个解决方案..令人沮丧...我没有得到编辑的任何回应....真令人沮丧:(
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多