2011-06-22 43 views
3
[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(); 
} 

調用方法:如何避免HTML腳本時,Web服務將返回

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" /> 

我要什麼做。

+0

實際格式我需要的是:<消息loginstatus = 「OK」 的用戶ID = 「1」/> –

+0

但我得到值是這樣的:&gtmessage loginstatus = 「OK」 的用戶ID = 「1」/&GT –

回答

2
[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; 
} 
+0

最後我喜歡soln..frustrating ...我沒有得到編輯的任何反應....真的很沮喪:( –