• XDocument 分页


    初试用XDocument来分页,代码如下,用在WebService里面,但框架需要4.0以上,用来读取XML,拼接成Json

        private string FilterJSON(string str)
        {
            return str.Replace("'", "%27").Replace("\"", "%22");
        }
        [WebMethod]
        public string GetPageNews(string pageNum, string pageRowCount)
        {
            int nPageNum = 0;
            int nPageRowCount = 0;
            if (!int.TryParse(pageNum, out nPageNum) || !int.TryParse(pageRowCount, out nPageRowCount))
            {
                return string.Empty;
            }
            XDocument doc = XDocument.Load(@"D:\Test\News.xml");
            var data = from d in doc.Descendants("News")
                       select d;
            var subData = data.Skip((nPageNum - 1) * nPageRowCount).Take(nPageRowCount);  // Paging.  
            StringBuilder sb = new StringBuilder();
            sb.Append("[");
            string strID = string.Empty;
            string strTitle = string.Empty;
            string strSummary = string.Empty;
            string strImageURL = string.Empty;
            string strDate = string.Empty;
            IEnumerable<XElement> dnas;
            #region 遍历
            foreach (var q in subData)
            {
                dnas = from node in q.Descendants() select node;
                foreach (XElement node in dnas)
                {
                    switch (node.Name.LocalName)
                    {
                        case "ID":
                            strID = node.Value;
                            break;
                        case "Title":
                            strTitle = node.Value;
                            break;
                        case "ImageURL":
                            strImageURL = node.Value;
                            break;
                        case "Summary":
                            strSummary = node.Value;
                            break;
                        case "Date":
                            strDate = node.Value;
                            break;
                        default:
                            break;
                    }
                }
                sb.AppendFormat("{{\"ID\":\"{0}\",\"标题\":\"{1}\",\"摘要\":\"{2}\",\"图片地址\":\"{3}\",\"发布日期\":\"{4}\"}}",
              strID, FilterJSON(strTitle), FilterJSON(strSummary), strImageURL, strDate);
                sb.Append(",");
            }
            return sb.ToString().Trim(',') + "]";
            #endregion
        }
        [WebMethod]
        public string GetNewsDetail(string id)
        {
    
            XDocument doc = XDocument.Load(@"D:\TestNews.xml");
            var data = from d in doc.Root.Descendants("News")
                       where d.Element("ID").Value == id.ToString()
                       select d;
            var subData = data.Take(1);
            StringBuilder sb = new StringBuilder();
            string strID = string.Empty;
            string strTitle = string.Empty;
            string strSource = string.Empty;//来源
            string strBody = string.Empty;//正文
            string strBImageURL = string.Empty;//大图地址
            string strSImageURL = string.Empty;//小图地址
            string strDate = string.Empty;
            IEnumerable<XElement> dnas;
            //Console.WriteLine("count:" + subData.Count());
            #region 遍历
            foreach (var q in subData)
            {
                dnas = from node in q.Descendants() select node;
                foreach (XElement node in dnas)
                {
                    switch (node.Name.LocalName)
                    {
                        case "ID":
                            strID = node.Value;
                            break;
                        case "Title":
                            strTitle = node.Value;
                            break;
                        case "Source":
                            strSource = node.Value;
                            break;
                        case "Body":
                            strBody = node.Value;
                            break;
                        case "BImageURL":
                            strBImageURL = node.Value;
                            break;
                        case "SImageURL":
                            strSImageURL = node.Value;
                            break;
                        case "Date":
                            strDate = node.Value;
                            break;
                        default:
                            break;
                    }
                }
                sb.AppendFormat("{{\"ID\":\"{0}\",\"标题\":\"{1}\",\"发布时间\":\"{2}\",\"来源\":\"{3}\",\"正文\":\"{4}\",\"小图地址\":\"{5}\",\"大图地址\":\"{6}\"}}",
              strID, FilterJSON(strTitle), strDate, FilterJSON(strSource), FilterJSON(strBody), strSImageURL, strBImageURL);
            }
            #endregion
            return sb.ToString();
        }
        [WebMethod]
    

      

  • 相关阅读:
    【python】opencv教程CV2模块——翻转图片并保存
    【python】opencv教程CV2模块——简单画图
    【python】opencv教程CV2模块——复制图片、灰度模式并保存
    【python】opencv教程CV2模块——窗口显示图片、按键盘指定s键保存
    【python】opencv教程CV2模块——打开读取图片、窗口显示、保存图片
    【前端】一行代码实现网站网页变黑白灰效果
    【python】彩色图秒变酷炫黑白图,灰度模式,比PS还好用的图像处理,cv2
    【python】批量调整图片亮度和饱和度,比PS还方便的图片处理,cv2
    【python】使用滑动条调整图片亮度和饱和度,比PS还方便的图片处理,cv2
    代码-JS之正则验证手机格式
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2778501.html
Copyright © 2020-2023  润新知