又好久没有上来写文章了(为什么要说又呢???),最近比较忙,不过都是乱七八糟的事情,哎!
前一阵公司有个项目,用VS 2005开发的Web Service,有点心得,写下来,与君共勉!!!

      这次完全采用Schema来定义数据结构,以一个书目查询的服务为例!那么我们先来定义一个关于书籍信息的数据结构的Schema:

WebService开发心得<?xml version="1.0" encoding="UTF-8"?>
WebService开发心得
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:in="http://www.bookinfo.com/bookinfo/" targetNamespace="http://www.bookinfo.com/bookinfo/" elementFormDefault="qualified" attributeFormDefault="unqualified">
WebService开发心得    
<xs:complexType name="BookInfoType">
WebService开发心得        
<xs:sequence>
WebService开发心得            
<xs:element name="BookName" type="xs:string"/>
WebService开发心得            
<xs:element name="AuthorName" type="xs:string"/>
WebService开发心得            
<xs:element name="Price" type="xs:float"/>
WebService开发心得            
<xs:element name="Publish" type="xs:string"/>
WebService开发心得            
<xs:element name="PublishDate" type="xs:dateTime"/>
WebService开发心得        
</xs:sequence>
WebService开发心得    
</xs:complexType>
WebService开发心得    
<xs:element name="BookInfo" type="in:BookInfoType"/>
WebService开发心得
</xs:schema>
WebService开发心得


      之后我们再来定义一下Request协议的Schema

WebService开发心得<?xml version="1.0" encoding="utf-8"?>
WebService开发心得
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:ns1="http://www.bookinfo.com/bookinforequest/" targetNamespace="http://www.bookinfo.com/bookinforequest/" elementFormDefault="qualified" attributeFormDefault="unqualified">
WebService开发心得    
<xs:element name="QueryInfo">
WebService开发心得        
<xs:complexType>
WebService开发心得            
<xs:sequence>
WebService开发心得                
<xs:element name="BookName" type="xs:string" minOccurs="0"/>
WebService开发心得                
<xs:element name="AuthorName" type="xs:string" minOccurs="0"/>
WebService开发心得                
<xs:element name="PublishDate" minOccurs="0">
WebService开发心得                    
<xs:complexType>
WebService开发心得                        
<xs:sequence>
WebService开发心得                            
<xs:element name="StartDate" type="xs:dateTime"/>
WebService开发心得                            
<xs:element name="EndDate" type="xs:dateTime" minOccurs="0"/>
WebService开发心得                        
</xs:sequence>
WebService开发心得                    
</xs:complexType>
WebService开发心得                
</xs:element>
WebService开发心得            
</xs:sequence>
WebService开发心得        
</xs:complexType>
WebService开发心得    
</xs:element>
WebService开发心得
</xs:schema>
WebService开发心得

      好了,Request协议的结构定义完了,接下来就是Resposne的Schema了!

 

WebService开发心得<?xml version="1.0" encoding="utf-8"?>
WebService开发心得
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:in="http://www.bookinfo.com/bookinfo/" xmlns:r="http://www.bookinfo.com/bookinforesponse/" targetNamespace="http://www.bookinfo.com/bookinforesponse/" elementFormDefault="qualified" attributeFormDefault="unqualified">
WebService开发心得    
<!-- 引用Schema -->
WebService开发心得    
<xs:import namespace="http://www.bookinfo.com/bookinfo/" schemaLocation="D:\文档\Document\BookInfo.xsd"/>
WebService开发心得    
<!--引用Schema结束-->
WebService开发心得    
<!--数据类型定义-->
WebService开发心得    
<!--书籍信息列表-->
WebService开发心得    
<xs:complexType name="BookInfoListType">
WebService开发心得        
<xs:sequence>
WebService开发心得            
<xs:element name="BookInfo" type="in:BookInfoType" maxOccurs="unbounded"/>
WebService开发心得        
</xs:sequence>
WebService开发心得    
</xs:complexType>
WebService开发心得    
<!--书籍信息列表结束-->
WebService开发心得    
<!--数据类型定义结束-->
WebService开发心得    
<!--内容-->
WebService开发心得    
<xs:element name="BookInfoList" type="r:BookInfoListType"/>
WebService开发心得    
<!--内容-->
WebService开发心得
</xs:schema>
WebService开发心得


      Schema都定义完成了,我们可以使用VS自带的工具xsd.exe来生成XML序列化过的类文件了,xsd.exe会将Schema里的文档部分的根节点生成类,并将其它的element及attribute序列化为子类及属性


      将生成的类文件加到我们的项目里,开始WebService的编写

      首先是WebMethod的定义

      [WebMethod]
      public BookInfoListType BookInfoRequest(QueryInfo query) 
      {
           return null;      
      }
   
      这里比较特殊的就是方法的返回数据类型及参数数据类型,返回的数据类型为BookInfoListType,而参数的数据类型为QueryInfo,这两个类型,都是我们用Schema生成的类型,可以根据类实例属性的值,生成符合Schema要求的XML串,也可以自动解析符合Schema定义的XML字符串,将XML串里的数据信息,赋给类实例的相应属性
      当我们调用WebService时,.Net会将Post过来的XML字符串进行解析,并将XML字符串里的值,赋给类实例里的相应的属性,我们再通过取得的属性值进行相关操作
      在设置返回数据类型的类实例时,可以将想要返回的值赋给类实例,之后return这个实例就可以了,.net会
将这个XML序列化过的类实例生成XML串,并Response

      附上程序实例 WebSite



 

相关文章:

  • 2021-11-27
  • 2021-11-13
  • 2021-11-04
  • 2021-12-26
  • 2021-12-11
  • 2021-04-16
  • 2021-09-25
  • 2021-09-25
猜你喜欢
  • 2021-09-22
  • 2021-11-08
  • 2021-11-02
  • 2021-11-04
  • 2021-11-02
  • 2021-11-02
  • 2021-11-05
  • 2021-11-27
相关资源
相似解决方案