0
我正在为使用c#的公司编写Web服务。所以我有一个测试页面default.aspx。 从aspx页面我有一个输入名称=“zip”,我需要传递给web服务。 我无法弄清楚如何获取发布的数据。 Webservice将根据以xml形式传递的邮政编码运行sql语句。然后返回邮政编码xml作为回应。c#asmx web服务。如何从aspx页面读取发布数据
代码来自aspx页面下方。
string sendXml = String.Format(@"<?xml version=""1.0"" encoding=""utf-8""?>,
<zip>{0}</zip>",
Request["zip"]);
string postXml = "xmlRequest=" + HttpUtility.UrlEncode(sendXml);
HttpWebRequest req = (HttpWebRequest)WebRequest.Create("http://localhost:51857/zipfinder.asmx/showRecords");
req.Method = "POST";
req.ContentLength = postXml.Length;
req.ContentType = "application/x-www-form-urlencoded";
using (StreamWriter writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(postXml);
writer.Close();
}
var result = "";
try
{
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
string getResult = reader.ReadToEnd();
Response.Write(getResult);
}
}
catch (Exception ex)
{
Response.Write("error: " + ex.ToString());
}
那么的.asmx页面上
[ScriptMethod(ResponseFormat = ResponseFormat.Xml)]
public DataSet getZip() {
DataSet dataSet = db.QueryDataRows("*", "zip_codes_simple", "zip = '00501' or zip = '00544'", "", "zip_codes_simple");
return dataSet;
}
[WebMethod]
public XmlDocument showRecords(){
DataSet dataSet = getZip();
string recs = "";
XmlDocument doc = new XmlDocument();
XmlDeclaration dec = doc.CreateXmlDeclaration("1.0", null, null);
doc.AppendChild(dec);
XmlNode root = doc.CreateElement("zipcodes");
doc.AppendChild(root);
XmlElement zip = doc.CreateElement("zip");
XmlElement type = doc.CreateElement("type");
XmlElement primaryCity = doc.CreateElement("primaryCity");
XmlElement state = doc.CreateElement("state");
XmlElement latitude = doc.CreateElement("latitude");
XmlElement longitude = doc.CreateElement("longitude");
foreach (DataTable dt in dataSet.Tables)
{
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
zip.InnerText = dr["zip"].ToString();
root.AppendChild(zip);
type.InnerText = dr["type"].ToString();
root.AppendChild(type);
primaryCity.InnerText = dr["primary_city"].ToString();
root.AppendChild(primaryCity);
state.InnerText = dr["state"].ToString();
root.AppendChild(state);
latitude.InnerText = dr["latitude"].ToString();
root.AppendChild(latitude);
longitude.InnerText = dr["longitude"].ToString();
root.AppendChild(longitude);
//recs = "<p>" + dr["zip"] + ", " + dr["primary_city"] + "</p>";
//Response.Write("<p>" + dr["zip"] + " in " + dr["state"]);
}
}
else
{
//recs = "<p style='color: red;'>Invalid user name or password</p>";
}
}
return doc;
}
我怎样才能从ASPX页面获得邮政编码传递给查询,其中拉链=“” 我在寻找的东西阅读格式化的字符串并从传入的xml文档中提取邮政编码。
任何帮助表示赞赏。 谢谢
是的,它是因为我们有一个不支持内联服务器标签的cms。在asmx文件中有没有方法可以调用xml邮政编码。 aspx中的示例我可以使用Request [“field”],但是,我还没有看到引用xml的方法。基本上我需要读取该xml的内容,读取一个字段并在sql中使用值声明。 –
好的,所以'Request [“zip”]'正在工作?如果不是,请尝试'Request.Form [“zip”]'。要从XML文档中提取信息,可以使用xPath,例如'/ zipcodes/zip'将匹配'zipcode'内的所有'zip'。由于变量'string getResult = reader.ReadToEnd();'包含响应XML,因此可以将其加载到XmlDocument中,然后使用doc.SelectNodes(“/ xpath/to/node”)执行查询。我不确定我是否理解你的问题。 –