C#开发XMLWebService接⼝(SOAP)
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using BX.Interface;
namespace WebService
{
/
//<summary>
/// Service1 的摘要说明。
///</summary>
public class doService : System.Web.Services.WebService
{
///<summary>
///⽹上报销系统WebService服务通⽤调⽤⽅法
///</summary>
///<param name="ifCode">接⼝标识</param>
///<param name="ifPass">校验码</param>
///<param name="ifEvent">操作⽅式(可空)</param>
/
//<param name="ifInfo">传⼊的XML信息</param>
///<returns></returns>
[WebMethod]
public string IfService(string ifCode,string ifPass,string ifEvent,string ifInfo)
{
ifCode = ifCode.Trim();
ifPass = ifPass.Trim();
ifEvent = ifEvent.Trim();
ifInfo = ifInfo.Trim();
string IReceiveContract_Id = System.Configuration.ConfigurationSettings.AppSettings["IReceiveContract_Id"].Trim();
string IReceiveContract_PSW = System.Configuration.ConfigurationSettings.AppSettings["IReceiveContract_PSW"].Trim();
string IReceiveNoContract_Id = System.Configuration.ConfigurationSettings.AppSettings["IReceiveNoContract_Id"].Trim();
string IReceiveNoContract_PSW = System.Configuration.ConfigurationSettings.AppSettings["IReceiveNoContract_PSW"].Trim();
if(ifCode.Equals(IReceiveContract_Id))
{
if(!ifPass.Equals(IReceiveContract_PSW))
ateResultXML(2,"","");
return IApplyContract.ReceiveApply(ifCode,ifInfo);
}
else if(ifCode.Equals(IReceiveNoContract_Id))
{
if(!ifPass.Equals(IReceiveNoContract_PSW))
ateResultXML(2,"","");
return IApplyNoContract.ReceiveApply(ifCode,ifInfo);
}
else
{
ateResultXML(1,"","");
}
} //-- end IfSerive
}
}
// ****************************************************
//    功能:SOAP XML WebService接⼝通⽤处理类
//    说明:
//    创建者:dingzh@jstrd
//    创建时间:2007-07-04
//    修改信息:
// ****************************************************
using System;
using System.Xml;
using System.Web.Services.Description;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.Data;
namespace BX.Interface
{
///<summary>
/// SOAP XML WebService接⼝通⽤处理类
///</summary>
public class ICommon
{
public ICommon()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///<summary>
///动态引⽤⼀个XML Web Service
/// IfSerive(string ifCode,string ifPass,string ifEvent,string ifInfo)
///</summary>
///<param name="v_WSDL">WSDL路径</param>
/
//<param name="SvClsName">接⼝服务真实类名</param>
///<param name="v_WebMethod">接⼝函数⽅法名</param>
///<param name="v_ifCode">接⼝标识</param>
///<param name="v_args">接⼝函数参数列表数组</param>
///<returns>string</returns>
public static string getSoapService(string v_WSDL, string SvClsName, string v_WebMethod, string v_ifCode,ref object[] v_args)//不需安装SOAP ToolKit 3.0 !
{
string v_NameSpace = "DSv_"+v_ifCode;
string v_ClassName = v_NameSpace + "." + SvClsName;
//1)⽤XML阅读器从⼀个⽂件路径或者是⽹络路径中读⼊WSDL⽂件:
XmlTextReader reader = new XmlTextReader(v_WSDL);//加⼊ using System.Xml;
//2〉把读到的内容⽤ServiceDescription来描述:
ServiceDescription sd = ServiceDescription.Read(reader);  //加⼊using System.Web.Services.Description;
//3)⽤服务导⼊器把得到的描述导⼊服务描述中:
ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();
sdi.AddServiceDescription(sd,null,null);
//4)指定要动态编译的代码的命名空间:
CodeNamespace cn = new CodeNamespace(v_NameSpace);  //"DynamicServices"
//5)指定要动态编译的代码单元:
CodeCompileUnit ccu = new CodeCompileUnit();
//6)把命名空间添加到动态编译的代码单元的命名空间组中去:
ccu.Namespaces.Add(cn);
//7)指⽰服务导⼊器把该动态编译单元内该命名空间的代码导进去,作为新的动态服务描述:
sdi.Import(cn,ccu);
//8)新建⼀个C#编译器来编译:
ICodeCompiler icc = new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
//9)新建⼀个编译参数对象(在这⾥我们设置它为空的编译参数):
CompilerParameters cp=new CompilerParameters();
//10)指⽰C#编译器,按照 CP 编译参数对象提供的参数来进⾏ CCU 单元的编译。并把编译结果传给编译结果对象 cr:
CompilerResults cr = icc.CompileAssemblyFromDom(cp, ccu);
//11)从动态编译结果中获取某个字符串(通常是该类型名字)所表⽰的真实类:
Type t = cr.CompiledAssembly.GetType(v_ClassName);  //其中的DynamicServices必须和
CodeNamespace cn = new  CodeNamespace("DynamicServices");定义的同名
//12)创建该类的实例:
Object obj = Activator.CreateInstance(t);
//13)根据⽅法名称字符串反射到该⽅法的实体:
System.Reflection.MethodInfo mi = obj.GetType().GetMethod(v_WebMethod);
//14)⽤该⽅法实体的.Invoke()来调⽤该⽅法:
string ret = System.Convert.ToString(mi.Invoke(obj, v_args));  //,new object[]{"传给⽅法的参数1","传给⽅法的参数2"}));
return ret;
//            添加Web 引⽤,在WSDL⼀栏中把刚才得到的WSDL地址复制过来,Web 引⽤的名称输⼊JavaService
//            JavaService.ServiceService bean = new JavaService.ServiceService();
//            string aa = bean.IfService("ifUser","ifPass","ifCode","ifEvent","info");
}  //-- End getSoapService/
///<summary>
///创建返回结果XML
///</summary>
///<param name="v_ifResult">接⼝结果代码</param>
///<param name="v_ifStack">错误信息堆栈</param>
///<param name="v_ifResultInfo">接⼝结果信息</param>
///<returns>string</returns>
public static string createResultXML(int v_ifResult,string v_ifErrStack,string v_ifResultInfo)        {
string v_ifError = "";
switch(v_ifResult)
{
case 1:
v_ifError = "⽆此接⼝标识ifCode";
break;
case 2:
v_ifError = "调⽤系统校验未通过";
break;
case 3:
v_ifError = "传⼊的不是⼀个正确的xml格式";
break;
case 4:
v_ifError = "info参数中的字段不符合规约中的要求";
break;
case 5:
v_ifError = "该条数据已重复发送";
break;
case 9:
v_ifError = "本系统处理时发⽣异常";
break;
default:
break;
}
XmlDocument xmldoc;
//XmlNode xmlnode;
XmlElement xmlelem;
xmldoc = new XmlDocument();
//加⼊XML的声明段落,<?xml version="1.0" encoding="gb2312"?>
XmlDeclaration xmldecl;
xmldecl = xmldoc.CreateXmlDeclaration("1.0","gb2312",null);
xmldoc.AppendChild (xmldecl);
//加⼊⼀个根元素
xmlelem = xmldoc.CreateElement ("","IfResponse","");
xmldoc.AppendChild (xmlelem) ;
/
/加⼊另外⼀个元素
//for(int i=1;i<3;i++)
//{
XmlNode root = xmldoc.SelectSingleNode("IfResponse");//查<IfResponse>
XmlElement eIfResult = xmldoc.CreateElement("IfResult");//创建⼀个<IfResult>节点
eIfResult.SetAttribute("Note","接⼝结果代码"); //设置该节点Note属性
eIfResult.InnerText = v_ifResult.ToString(); //设置节点⽂本值
root.AppendChild(eIfResult);  //添加到<IfResponse>节点中
XmlElement eIfError = xmldoc.CreateElement("IfError");
eIfError.SetAttribute("Note","错误信息");
eIfError.InnerText = v_ifError;
root.AppendChild(eIfError);
XmlElement eIfErrorStack = xmldoc.CreateElement("IfErrorStack");
eIfErrorStack.SetAttribute("Note","错误信息堆栈");
eIfErrorStack.InnerText = v_ifErrStack;
root.AppendChild(eIfErrorStack);
XmlElement eIfResultInfo = xmldoc.CreateElement("IfResultInfo");
eIfResultInfo.SetAttribute("Note","接⼝结果信息");
eIfResultInfo.InnerText = v_ifResultInfo;
root.AppendChild(eIfResultInfo);
//}
//保存创建好的XML⽂档
/
/xmldoc.Save ( Server.MapPath("l") ) ;
return xmldoc.InnerXml;
//xmldoc.OuterXml;
//        <?xml version="1.0" encoding="GB2312"?>
//        <IfResponse>
//        <IfResult>接⼝结果代码</IfResult>
//        <IfError>错误信息</IfError>
//        <IfErrorStack>错误信息堆栈</IfErrorStack>
//        <IfResultInfo>接⼝结果信息</IfResultInfo>
//        </IfResponse>
//调用webservice服务
/
/        <IfResult/>依照下述约定:
//        成功:0 —仅表⽰接⼝调⽤成功,并不代表业务调⽤成功,业务返回结果信息存放于<IfResultInfo/>节点中
//        失败:⾮零整数值
//            1:⽆此接⼝标识ifCode
//            2:调⽤系统校验未通过
//            3:传⼊的不是⼀个正确的xml格式
//            4:info参数中的字段不符合规约中的要求
//            5:该条数据已重复发送
//            9:本系统处理时发⽣异常
} //-- End createResultXML/
///<summary>
///将XML格式字符串转化成DataSet
///</summary>
///<param name="xml">xml字符串</param>
///<returns>DataSet</returns>
public static DataSet parseResultXML2DataSet(string xml)
{
DataSet ds = new DataSet();
//⽅法⼀
using(System.IO.StringReader sr = new System.IO.StringReader(xml))
{
ds.ReadXml(sr);
}
//            //⽅法⼆
//            using(System.Xml.XmlReader xr = System.Xml.XmlReader.Create(new System.IO.StringReader(xml))) //            {
//                ds.ReadXml(xr);
//            }
//            //⽅法三
//            using(System.IO.Stream st = new System.IO.MemoryStream(System.Text.Encoding.UTF8.GetBytes(xml))) //            {
//                ds.ReadXml(st);
//            }
return ds;
}
///<summary>
///将XML格式字符串转化成IStructResult
///</summary>
///<param name="xml">xml字符串</param>
///<returns>IStructResult</returns>
public static IStructResult parseResultXML2Struct(string xml)
{
IStructResult structResult = new IStructResult();
structResult.Clear();
XmlDocument xmldoc = new XmlDocument();
try
{
xmldoc.LoadXml(xml);
}
catch(XmlException ex)
{
structResult.IfResult = -1;
structResult.IfError = "未能处理的异常返回结果";
structResult.IfErrorStack = ex.Message;
return structResult;
//throw ex;
}
try
{
string strResult = xmldoc.GetElementsByTagName("IfResult").Item(0).InnerText.Trim();
structResult.IfResult        = strResult.Equals(string.Empty)?-1:int.Parse(strResult);
structResult.IfError        = xmldoc.GetElementsByTagName("IfError").Item(0).InnerText.Trim();
structResult.IfErrorStack    = xmldoc.GetElementsByTagName("IfErrorStack").Item(0).InnerText.Trim();                structResult.IfResultInfo    = xmldoc.GetElementsByTagName("IfResultInfo").Item(0).InnerText.Trim();            }
catch(Exception e)
{
structResult.IfResult = -1;
structResult.IfError = "未能处理的异常返回结果";
structResult.IfErrorStack = e.Message;
return structResult;
//throw e;
}
return structResult;
}
}
///<summary>
///接⼝返回结果结构体
/
//</summary>
public struct IStructResult
{
public int        IfResult;
public string    IfError;
public string    IfErrorStack;
public string    IfResultInfo;
public void Clear()
{
IfResult        = -1;
IfError            = "";
IfErrorStack    = "";
IfResultInfo    = "";
}
}
}
using System;
using System.Xml;
using System.IO;
namespace BX.Interface
{
///<summary>
/
// IApplyContract 的摘要说明。
///</summary>
public class IApplyContract
{
public IApplyContract()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
///<summary>
/
//接收合同付款申请信息
///</summary>
///<param name="xml"></param>
///<returns></returns>
public static string ReceiveApply(string interfaceid, string xml)
{
XmlDocument xmldoc = new XmlDocument();

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。