程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> WebService傳遞XML數據 C#DataSet操作XML 解析WebService返回的XML數據

WebService傳遞XML數據 C#DataSet操作XML 解析WebService返回的XML數據

編輯:C#入門知識

Webservice傳遞的數據只能是序列化的數據,典型的就是xml數據。


 

// <summary>  
       /// 通過用戶名和密碼 返回下行數據  
       /// </summary>  
       /// <param name="UserName">用戶名</param>  
       /// <param name="UserPwd">密碼</param>  
       /// <returns></returns>  
       [WebMethod] 
       public XmlDataDocument GetUpMassageDate(string UserName, string UserPwd) 
       { 
           try 
           { 
               XmlDataDocument xd = new XmlDataDocument(); 
               DataSet ds = DbHelperSQL.Query("select   Mobile,UPMessge, RecordDate from dbo.NA_Activity_Data where ActivityID in( select ActivityID from dbo.NA_Activity  where UserID in (select UserID from dbo.NA_User  where UserName='" + UserName.Trim() + "' and UserPwd='" + UserPwd.Trim() + "'))"); 
               if (ds != null && ds.Tables.Count > 0) 
               { 
                   xd = new XmlDataDocument(ds); 
                   XmlNode root1 = xd.DocumentElement; 
 
                   XmlNodeList roots = root1.SelectNodes("ds"); 
                   foreach (XmlNode item in roots) 
                   { 
                       XmlNodeList list = item.SelectNodes("RecordDate"); 
                       ds.EnforceConstraints = false;  //如果需要修改xml裡的數據  需要加上這句  
                       foreach (XmlNode node in list) 
                       { 
                           //這裡是修改XML中 RecordDate的時間格式 原始格式是:  <RecordDate>2012-04-20T16:16:00+08:00</RecordDate>   
                           node.InnerText = Convert.ToDateTime(node.InnerText.ToString()).ToString("yyyy-MM-dd HH:mm");   
                       } 
                   } 
                   return xd; 
               } 
               else 
               { 
                   return null; 
               } 
           } 
           catch (Exception ex) 
           { 
               return null; 
           } 
       } 

 /// <summary>
        /// 通過用戶名和密碼 返回下行數據
        /// </summary>
        /// <param name="UserName">用戶名</param>
        /// <param name="UserPwd">密碼</param>
        /// <returns></returns>
        [WebMethod]
        public XmlDataDocument GetUpMassageDate(string UserName, string UserPwd)
        {
            try
            {
                XmlDataDocument xd = new XmlDataDocument();
                DataSet ds = DbHelperSQL.Query("select   Mobile,UPMessge, RecordDate from dbo.NA_Activity_Data where ActivityID in( select ActivityID from dbo.NA_Activity  where UserID in (select UserID from dbo.NA_User  where UserName='" + UserName.Trim() + "' and UserPwd='" + UserPwd.Trim() + "'))");
                if (ds != null && ds.Tables.Count > 0)
                {
                    xd = new XmlDataDocument(ds);
                    XmlNode root1 = xd.DocumentElement;

                    XmlNodeList roots = root1.SelectNodes("ds");
                    foreach (XmlNode item in roots)
                    {
                        XmlNodeList list = item.SelectNodes("RecordDate");
                        ds.EnforceConstraints = false;  //如果需要修改xml裡的數據  需要加上這句
                        foreach (XmlNode node in list)
                        {
                            //這裡是修改XML中 RecordDate的時間格式 原始格式是:  <RecordDate>2012-04-20T16:16:00+08:00</RecordDate>
                            node.InnerText = Convert.ToDateTime(node.InnerText.ToString()).ToString("yyyy-MM-dd HH:mm"); 
                        }
                    }
                    return xd;
                }
                else
                {
                    return null;
                }
            }
            catch (Exception ex)
            {
                return null;
            }
        }

引用webservice返回XML數據,解析XML數據並綁定到GridView上

public void DateBind() 
       { 
           UpMassgeWebserive.GetDateUpMassageSoapClient um = new UpMassgeWebserive.GetDateUpMassageSoapClient(); 
           DataSet ds = new DataSet(); 
           XmlNode xmlNode1; 
           XmlDataDocument xd = new XmlDataDocument(); 
           StringBuilder sb; 
           xmlNode1 = um.GetUpMassageDate("hzh", "chinahzh"); 
           if (xmlNode1==null) 
           { 
               return; 
           } 
           sb = new StringBuilder(xmlNode1.OuterXml); 
           if (sb.ToString().Equals("")) 
           { 
               return; 
           } 
           xd.LoadXml(sb.ToString()); 
           ds.ReadXml(new XmlNodeReader(xd)); 
 
           GridView1.DataSource = ds.Tables[0].DefaultView; 
           GridView1.DataBind(); 

 

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