程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> 關於.NET >> C# WebServices中處理XML方式,

C# WebServices中處理XML方式,

編輯:關於.NET

C# WebServices中處理XML方式,


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;
            }
        }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved