由于自身知识限制,并不知道这么做对不对,仅供参考。
我理解的webservice就是放在网络上的一个函数。
那下面简单实现下这个功能:
两台电脑1和2。
我们的订单放在电脑1上,提供给了前台查询订单的一个函数order_query(),电脑2要调用这个函数来判断用户有无下单。
下面是电脑1上的asmx文件,假设订单信息用xml保存。
using System;
using System.Xml;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace WebApplication2
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string[] order_query(string cus_name, string cus_id)
{
//初始化一个xml实例
XmlDocument myXmlDoc = new XmlDocument();
//加载xml文件(参数为xml文件的路径)
myXmlDoc.Load("C:\\Users\\DELL\\Desktop\\web\\WebApplication2\\WebApplication2\\XMLFile1.xml");
XmlNode rootNode = myXmlDoc.SelectSingleNode("order_summary");
//分别获得该节点的InnerXml和OuterXml信息
string innerXmlInfo = rootNode.InnerXml.ToString();
string outerXmlInfo = rootNode.OuterXml.ToString();
string[] r = new string[30];
int i = 0;
XmlNodeList firstLevelNodeList = rootNode.ChildNodes;//customer
for (int m = 0; m < firstLevelNodeList.Count; m++)
{
XmlNodeList tepNodeList = firstLevelNodeList[m].ChildNodes;//customer的子节点
if (tepNodeList[0].ChildNodes[0].InnerText != cus_name || tepNodeList[0].ChildNodes[1].InnerText != cus_id) continue;
foreach (XmlNode node in tepNodeList)
{//node=cus_info,order,detail
if (node.HasChildNodes)
{
int tep = node.ChildNodes.Count;
for (int j = 0; j < tep; j++)
{
if (tep != 1)
{
r[i] = node.ChildNodes[j].Name + ": ";
if (node.ChildNodes[j].ChildNodes.Count != 1)
{
foreach (XmlNode tepNode in node.ChildNodes[j].ChildNodes)
{
r[i] += tepNode.Name + ":" + tepNode.InnerText + " ";
}
}
else r[i] += node.ChildNodes[j].InnerText;
i++;
}
else r[i++] = node.ChildNodes[j].InnerText;
}
}
}
}
string[] ans;
if (i == 0)
{
ans = new string[1];
ans[0] = "未找到该用户";
}
else
{
ans = new string[i];
for (int j = 0; j < i; j++) ans[j] = r[j];
}
return ans;
}
}
}
xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="style1.xsl"?>
<order_summary>
<customer>
<cus_info>
<cus_name>球球</cus_name>
<cus_id>94188</cus_id>
<cus_phone>110110110</cus_phone>
<cus_address>中国台湾高雄</cus_address>
</cus_info>
<detail>
<order_id>013254889974</order_id>
<pay_way>支付宝</pay_way>
<pay_time>2018.05.31 16:40</pay_time>
</detail>
<order>
<food>
<name>鸡肉味狗粮</name>
<price>20元/g</price>
<money>500元</money>
<description>美味鸡肉狗粮,爱不释口</description>
</food>
<food>
<name>大海猫粮</name>
<price>30元/g</price>
<money>900元</money>
<description>让猫拥有大海般的感觉</description>
</food>
</order>
<tot_price>1400元</tot_price>
<state>true</state>
</customer>
<customer>
<cus_info>
<cus_name>彤彤</cus_name>
<cus_id>94187</cus_id>
<cus_phone>17867842272</cus_phone>
<cus_address>中国青岛烟台36号</cus_address>
</cus_info>
<detail>
<order_id>013254889979</order_id>
<pay_way>null</pay_way>
<pay_time>null</pay_time>
</detail>
<order>
<food>
<name>炸鸡腿</name>
<price>20元/个</price>
<money>20元</money>
<description>美味鸡肉</description>
</food>
<food>
<name>螺蛳粉</name>
<price>10元/碗</price>
<money>10元</money>
<description>酸酸爽爽的粉粉</description>
</food>
</order>
<tot_price>30元</tot_price>
<state>false</state>
</customer>
</order_summary>
我们对网站进行发布之后,还要进行授权,否则会访问失败。这里为了简单,设为所有人均可访问。
对这个xml文件,右击,安全,这样就可以访问了。
编辑代码试试:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
WebReference.WebService1 am = new WebReference.WebService1();
string[] ab=am.order_query("球球", "94188");
for (int i = 0; i < ab.Length; i++)
{
Console.WriteLine(ab[i]);
}
}
}
}
截图如下,