1、企业系统集成的时候,大量的字段是很麻烦处理的,用Format 的方式可快速处理
string sql = @"SELECT * FROM table WHERE TASKID='" + taskid + "'";
DataTable dt = DBUtil.Query(sql).Tables[0];
//此处是获取XML模板的路径
Stream xmlStream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BPMWinService.Xml." + xmlname + ".xml");
XmlDocument xml = new XmlDocument();
xml.Load(xmlStream);
System.Xml.XmlNamespaceManager ns = new XmlNamespaceManager(xml.NameTable);
ns.AddNamespace("soap", "http://地址");
ns.AddNamespace("ns2", "http://地址/");
XmlNode InputParameters = xml.SelectSingleNode("soap:Envelope/soap:Body/ns2:InputParameters", ns);
string mModel = InputParameters.InnerXml;
InputParameters.RemoveAll();
//此处使用Format去设置其中的InnerText值,注意InnerXml,InnerText,OuterXml的意义和用法
//Format在循环中更有意义,只是需要先去设置XML的模板
mModel = string.Format(mModel, Convert.ToString(dt.Rows[0]["invoice_id"]), Convert.ToInt32(dt.Rows[0]["taskid"]), state);
InputParameters.InnerXml = mModel;
//System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
//stopwatch.Start();
WebServiceCaller wsCaller = new WebServiceCaller();
string isSaveXML = System.Configuration.ConfigurationManager.AppSettings["IsSaveXML"];
if (isSaveXML == "true")
xml.Save(@"C:/BPMWinService/ERP_BPMFP_XML.xml");
//自己调用别人接口的方法
string strReturnValue = wsCaller.QuerySoapWebService(url, YZHelper.ERP_BPMFP, xml);
//stopwatch.Stop();
XmlDocument returnDoc = new XmlDocument();
returnDoc.LoadXml(strReturnValue);
//这里不知道为什么获取不到节点,没整明白,就用一下的方式获取节点列表,再做循环去获取节点值,LastChild 的个数和返回的XML层级有关,有几层就写
//几个LastChild
XmlNodeList XNL = returnDoc.LastChild.LastChild.LastChild.ChildNodes;
XmlNode _Message = null;
string massage = string.Empty;
for (int i = 0; i < XNL.Count; i++)
{
if (Convert.ToString(XNL[i].Name) == "***")
{
_Message = XNL[i];
if (_Message.InnerText == "E")
{
for (int j = 0; j < XNL.Count; j++)
{
if (Convert.ToString(XNL[j].Name) == "***")
{
massage = XNL[j].InnerText;
break;
}
}
}
break;
}
}
/// <summary>
/// SOAP协议调用webservice
/// </summary>
private string QuerySoapWebService(String url, String methodName, string xmlNs, XmlDocument xml)
{
try
{
XML_NAMESPACE[url] = xmlNs;
byte[] data;
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
// ????head
request.Method = "POST";
request.ContentType = "text/xml; charset=utf-8";
request.AutomaticDecompression = DecompressionMethods.GZip;
string username = YZHelper.UserName;
string password = YZHelper.PassWord;
request.Credentials = new NetworkCredential(username, password);
using (MemoryStream ms = new MemoryStream())
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xml.InnerXml.ToString());
xmlDoc.Save(ms);
data = ms.ToArray();
}
//// ?soap ????????
WriteRequestData(request, data);
String retXml = string.Empty;
HttpWebResponse myrespones;
try
{
myrespones = (HttpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
myrespones = (HttpWebResponse)ex.Response;
}
retXml = ReadMotmResponse(myrespones); //??Webservice????
//??????
if (request != null)
{
request.Abort();
request = null;
}
if (myrespones != null)
{
myrespones.Close();
myrespones = null;
}
return retXml;
}
catch (Exception e)
{
return e.Message;
}
}