目录
3.1.2 自定义方法--使用Description添加方法注释
3.1.4 启动Internet Information Service
3.3.1 采用高级模式(可解决老的web服务可能存在的不兼容问题)
1 XML基本概念
可扩展标记语言(英语:Extensible Markup Language,简称:XML),是一种标记语言。标记指计算机所能理解的信息符号,通过此种标记,计算机之间可以处理包含各种信息的文章等。
如何定义这些标记,既可以选择国际通用的标记语言,比如HTML,也可以使用像XML这样由相关人士自由决定的标记语言,这就是语言的可扩展性。
XML是从标准通用标记语言(SGML)中简化修改出来的。
文档中必须有一个根标签
<?xml version="1.0">
<students>
<student>
<name>xiaoming</xiaoming>
<student address="xian"> <!---单独的标签有结束的标签--->
</student>
</students>
2 使用C#操作xml
需要的名称空间:using System.Xml
2.1 读写xml文件
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace xmlDemo
{
public class RWxml
{
public void WriteXml()
{
Random rd = new Random();
//创建一个xml
//实例化一个 XmlDocument 对象
XmlDocument xDoc = new XmlDocument();
//创建一个声明xml所需的语法变量
XmlDeclaration declartion = xDoc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
xDoc.AppendChild(declartion);
//创建一个根节点
XmlElement xe = xDoc.CreateElement("students");
xDoc.AppendChild(xe);
//使用循环添加多条数据
for (int i = 0; i < 10; i++)
{
//添加子节点
XmlElement xeChild = xDoc.CreateElement("student"+i.ToString());
xe.AppendChild(xeChild);
//XmlElement xeChildName = xDoc.CreateElement("name");
//xeChild.AppendChild(xeChildName);
//给节点添加属性
xeChild.SetAttribute("name", "张三");
xeChild.SetAttribute("class", rd.Next(100000, 999999).ToString());
//创建三级节点
XmlElement xeChildYuWen = xDoc.CreateElement("语文成绩");
xeChild.AppendChild(xeChildYuWen);
xeChildYuWen.InnerText = rd.Next(100).ToString();
XmlElement xeChildShuXue = xDoc.CreateElement("数学成绩");
xeChild.AppendChild(xeChildShuXue);
xeChildShuXue.InnerText = rd.Next(60, 100).ToString();
}
xDoc.Save("student.xml");
Console.WriteLine("保存成功");
}
public void ReadXml()
{
//实例化一个 XmlDocument 对象
XmlDocument xDoc = new XmlDocument();
xDoc.Load("student.xml");
//获取根节点
XmlNode node = xDoc.SelectSingleNode("students");
//获取子节点
XmlNodeList xnl = node.ChildNodes;
foreach (XmlNode nd in xnl)
{
XmlElement ndEle = nd as XmlElement;
//Console.WriteLine(ndEle.Name);
Console.Write(ndEle.GetAttribute("name") + "\t");
Console.WriteLine(ndEle.GetAttribute("class"));
foreach (XmlNode ndChild in nd.ChildNodes)
{
XmlElement ndChildEle = ndChild as XmlElement;
Console.WriteLine(ndChildEle.Name);
Console.WriteLine(ndChildEle.InnerText);
//可以修改数据
ndChildEle.InnerText = "100";
}
}
//如果修改必须调save方法
xDoc.Save("student.xml");
}
}
}
2.2 调用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace xmlDemo
{
class Program
{
static void Main(string[] args)
{
RWxml xml = new RWxml();
//xml.WriteXml();
xml.ReadXml();
}
}
}
2.3 结果
2.3.1 写出的结果
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<students>
<student0 name="张三" class="944514">
<语文成绩>53</语文成绩>
<数学成绩>62</数学成绩>
</student0>
<student1 name="张三" class="658669">
<语文成绩>38</语文成绩>
<数学成绩>85</数学成绩>
</student1>
<student2 name="张三" class="591099">
<语文成绩>20</语文成绩>
<数学成绩>78</数学成绩>
</student2>
<student3 name="张三" class="637884">
<语文成绩>88</语文成绩>
<数学成绩>71</数学成绩>
</student3>
<student4 name="张三" class="241673">
<语文成绩>95</语文成绩>
<数学成绩>70</数学成绩>
</student4>
<student5 name="张三" class="958220">
<语文成绩>73</语文成绩>
<数学成绩>66</数学成绩>
</student5>
<student6 name="张三" class="119730">
<语文成绩>48</语文成绩>
<数学成绩>83</数学成绩>
</student6>
<student7 name="张三" class="429732">
<语文成绩>74</语文成绩>
<数学成绩>93</数学成绩>
</student7>
<student8 name="张三" class="651381">
<语文成绩>83</语文成绩>
<数学成绩>69</数学成绩>
</student8>
<student9 name="张三" class="800010">
<语文成绩>40</语文成绩>
<数学成绩>85</数学成绩>
</student9>
</students>
2.3.2 读入的结果
3 Web服务
3.1 项目创建与方法定义
3.1.1 项目创建
系统生成代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
namespace myWebService1
{
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
}
}
3.1.2 自定义方法--使用Description添加方法注释
[WebMethod(Description="求和的方法")]
public int JiaFa(int x, int y)
{
return x + y;
}
一个挂在公网的天气预报的web服务
3.1.3 发布
3.1.4 启动Internet Information Service
全部勾选
输入127.0.0.1,弹出如下界面:
添加本地网站:
输入【主机名/Service1.asmx】,即可搭建成功
3.2 窗体程序调用web网站
3.2.1 添加Web服务引用
添加服务引用
3.2.2 查询一个省支持的城市
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using demoWeb.WeatherServiceReference;
namespace demoWeb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSearch_Click(object sender, EventArgs e)
{
try
{
//实例化类
WeatherWebServiceSoapClient wwssc = new WeatherWebServiceSoapClient();
string[] citys = wwssc.getSupportCity("辽宁");
foreach (string city in citys)
{
textBox1.AppendText(city + "\r\n");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
3.2.3 异常处置
多个终结点异常
处理方法
3.3 避免不兼容现象的引用方式
3.3.1 采用高级模式(可解决老的web服务可能存在的不兼容问题)
3.3.2 代码编写
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using demoWeb.Weather;
namespace demoWeb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void buttonSearch_Click(object sender, EventArgs e)
{
try
{
//实例化类
//WeatherWebServiceSoapClient wwssc = new WeatherWebServiceSoapClient();
//string[] citys = wws.getSupportCity("辽宁");
//foreach (string city in citys)
//{
// textBox1.AppendText(city + "\r\n");
//}
//string[] citys = wwssc.getSupportCity("辽宁");
Weather.WeatherWebService wws = new Weather.WeatherWebService();
string[] cityInfos = wws.getWeatherbyCityName("大连");
foreach (string info in cityInfos)
{
textBox1.AppendText(info + "\r\n");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
R 参考资料