C# 组装XML传给webserver+XML 返回获取多个xml,根据多个XML 返回dataset类型
大致流程介绍:
传值给 webserver+XML ,得到webserver+XML多个返回值,组装成dataset形式返回
首先创建所需要的类型
DataSet ds = new DataSet(); //表集合
DataTable dt = new DataTable; //表
dt.Columns.Add("ID"); 添加表的列名
dt.Columns.Add("Name"); 添加表的列名
DataRow dr = dt.NewRow(); //表行
try
{
//组装xml 传给webser+XMl需要的入参
string xml_PESelfReg_value = Assemble_XML.XML_BARCODE(PatientID);
1 public static string XML_BARCODE(string PEADM) 2 { 3 XDocument doc = new XDocument( 4 new XDeclaration("1.0", "utf-8", "yes"),///XML的声明,包括版本,编码,xml文件是否独立 5 new XElement("Request",new XElement("PEADM",""+PEADM+"")) 6 //根节点 7 8 9 ); 10 11 return doc.ToString(); 12 13 }
引用WEBserver帮助类
1 using System; 2 using System.Collections.Generic; 3 using System.Text; 4 5 using System.IO; 6 using System.Net; 7 using System.CodeDom; 8 using System.CodeDom.Compiler; 9 using Microsoft.CSharp; 10 using System.Web.Services.Description; 11 12 namespace Winning 13 { 14 class WebServiceHelper 15 { 16 /// <summary> 17 /// 动态调用WebService 18 /// </summary> 19 /// <param name="url">WebService地址</param> 20 /// <param name="methodname">方法名(模块名)</param> 21 /// <param name="args">参数列表,无参数为null</param> 22 /// <returns>object</returns> 23 public static string InvokeWebService(string url, string methodname, string strxml) 24 { 25 if (strxml == null) 26 return (string)InvokeWebService(url, null, methodname, null); 27 else 28 { 29 string[] s = { strxml }; 30 return (string)InvokeWebService(url, null, methodname, s); 31 } 32 } 33 /// <summary> 34 /// 动态调用WebService 35 /// </summary> 36 /// <param name="url">WebService地址</param> 37 /// <param name="classname">类名</param> 38 /// <param name="methodname">方法名(模块名)</param> 39 /// <param name="args">参数列表</param> 40 /// <returns>object</returns> 41 public static object InvokeWebService(string url, string classname, string methodname, object[] args) 42 { 43 string @namespace = "fangqm.Netbank.WebService.webservice"; 44 if (classname == null || classname == "") 45 { 46 classname = WebServiceHelper.GetClassName(url); 47 } 48 //获取服务描述语言(WSDL) 49 WebClient wc = new WebClient(); 50 Stream stream = wc.OpenRead(url + "?WSDL");//【1】 51 ServiceDescription sd = ServiceDescription.Read(stream);//【2】 52 ServiceDescriptionImporter sdi = new ServiceDescriptionImporter();//【3】 53 sdi.AddServiceDescription(sd, "", ""); 54 CodeNamespace cn = new CodeNamespace(@namespace);//【4】 55 //生成客户端代理类代码 56 CodeCompileUnit ccu = new CodeCompileUnit();//【5】 57 ccu.Namespaces.Add(cn); 58 sdi.Import(cn, ccu); 59 CSharpCodeProvider csc = new CSharpCodeProvider();//【6】 60 ICodeCompiler icc = csc.CreateCompiler();//【7】 61 //设定编译器的参数 62 CompilerParameters cplist = new CompilerParameters();//【8】 63 cplist.GenerateExecutable = false; 64 cplist.GenerateInMemory = true; 65 cplist.ReferencedAssemblies.Add("System.dll"); 66 cplist.ReferencedAssemblies.Add("System.XML.dll"); 67 cplist.ReferencedAssemblies.Add("System.Web.Services.dll"); 68 cplist.ReferencedAssemblies.Add("System.Data.dll"); 69 //编译代理类 70 CompilerResults cr = icc.CompileAssemblyFromDom(cplist, ccu);//【9】 71 if (true == cr.Errors.HasErrors) 72 { 73 System.Text.StringBuilder sb = new StringBuilder(); 74 foreach (CompilerError ce in cr.Errors) 75 { 76 sb.Append(ce.ToString()); 77 sb.Append(System.Environment.NewLine); 78 } 79 throw new Exception(sb.ToString()); 80 } 81 82 //生成代理实例,并调用方法 83 System.Reflection.Assembly assembly = cr.CompiledAssembly; 84 Type t = assembly.GetType(@namespace + "." + classname, true, true); 85 object bj = Activator.CreateInstance(t);//【10】 86 System.Reflection.MethodInfo mi = t.GetMethod(methodname);//【11】 87 return mi.Invoke(bj, args); 88 89 } 90 private static string GetClassName(string url) 91 { 92 //假如URL为"http://localhost/InvokeService/Service1.asmx" 93 //最终的返回值为 Service1 94 string[] parts = url.Split('/'); 95 string[] pps = parts[parts.Length - 1].Split('.'); 96 return pps[0]; 97 } 98 } 99 }
string responseXml = (string)WebServiceHelper.InvokeWebService(“”webser地址“”, "GetBarInfo", xml_PESelfReg_value);
try
{
XmlDocument xmldoc = new XmlDocument();
xmldoc.LoadXml(responseXml); 得到返回的responseXml
XmlNodeList xnList = xmldoc.SelectNodes("Response/LabList"); //找到从哪个节点开始重复
如<responese>
<LabList>
<a></a>
<b></b>
<c></b>
</LabList>
</responese>
foreach (XmlNode xn in xnList) //循环节点 获取值,并赋值给 表行
{
dr["ID"] = xn.SelectSingleNode("a").InnerText;//<LabList>下的节点a
dr["Name"] = xn.SelectSingleNode("b").InnerText;
dt.Rows.Add(dr); //表行数据给表dt
}
dt.TableName = "表名字"; 、、给表增加名字
ds.Tables.Add(dt.Copy()); 、、把dt表 结构copy到ds
}
catch (Exception e)
{
LogHelper.Info("有问题:" + e.ToString());
}
return ds;
本文来自博客园,作者:12不懂3,转载请注明原文链接:https://www.cnblogs.com/LZXX/p/7250670.html