C # write webservice (2)

Due to its own knowledge limitations, it is not known whether this is correct or not, and it is for reference only.
The webservice I understand is a function placed on the network.

Then simply implement this function below:
two computers 1 and 2.
Our order is placed on computer 1, which provides a function order_query () at the front desk to query orders. Computer 2 calls this function to determine whether the user has placed an order.


The following is the asmx file on computer 1, assuming that the order information is saved in 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 code:

<?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>

After we publish the website, we must also authorize it, otherwise the access will fail. For simplicity here, make it accessible to everyone.
Right-click on this xml file, safe, Insert picture description hereso that it can be accessed.
Edit the code and try:

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]);
           }
        }
    }
}

Screenshot below,Insert picture description here

Published 161 original articles · Like 68 · Visitors 20,000+

Guess you like

Origin blog.csdn.net/qq_43179428/article/details/105186184