zoukankan      html  css  js  c++  java
  • 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]
    

      

  • 相关阅读:
    String、StringBuffer、StringBuilder
    动态规划引入—矩阵乘法
    flask中间件
    有状态服务,无状态服务
    python 工厂模式
    python 单例模式
    python 工厂模式
    python timedelta() 和relativedelta()的区别
    mongo 查看(集合)表结构
    logstash 实现数据源分流
  • 原文地址:https://www.cnblogs.com/gzh4455/p/2778501.html
Copyright © 2011-2022 走看看