• linq to xml 操作实例


    /// <summary>
        /// 拼接xml结构
        /// </summary>
        /// <returns></returns>
        public XElement GetListXml(string id)
        {
            try
            {
                //判断客户端传输过来的是否是int型
                int tempInt = 0;
                int tempIntPs = 0;
                if (!string.IsNullOrEmpty(tempPageNumber))
                {
                    int.TryParse(tempPageNumber, out tempInt);
    
                }
                if (!string.IsNullOrEmpty(tempPageSize))
                {
                    int.TryParse(tempPageSize, out tempIntPs);
    
                }
                id = isBBSName(id);
                //
                XElement xRoot = new XElement("root");
    
                //查询根数据
                DataSet ds = null;
                if (string.IsNullOrEmpty(id))
                {
                    XElement xTabs = new XElement("tabs");
                    xRoot.Add(xTabs);
                    if (ds != null && ds.Tables[0].Rows.Count > 0)
                    {
                        foreach (DataRow dr in ds.Tables[0].Rows)
                        {
                            //选项卡
                            XElement xTab = new XElement("tab");
                            xTab.Add(new XAttribute("title", "" + dr["Name"].ToString().Trim() + ""));
                            xTab.Add(new XAttribute("id", "" + dr["HierarchyCode"].ToString().Trim() + ""));
                            xTabs.Add(xTab);
                        }
                    }
                    return xRoot;  //直接返回
                }
                else
                {
                    // 获取接口分类
                    SourceURL URL = new SourceURL("BBS_Category");
                    string ret = new GetHtmlByUrl().fncGetHtmlByUrl(URL["address"]);
                    XElement srcXml = XElement.Parse(ret);
                    var boards = from node in srcXml.Elements()
                                 where node.Name == "board"
                                 select node;
    
                    XElement imageView = new XElement("imageview");
                    xRoot.Add(imageView);
                    XElement list = new XElement("list");
                    foreach (XElement board in boards)
                    {
                        //  判断传输的id是否是大级分类
                        if (board.Attribute("name").Value == id)
                        {
                            var forums = from node in board.Elements()
                                         where node.Name == "forum"
                                         select node;
    
    
                            forums = forums.Skip(tempInt).Take(tempIntPs);
    
                            foreach (XElement forum in forums)
                            {
                                XElement item = new XElement("item");
                                if (!string.IsNullOrEmpty(forum.Attribute("id").Value))
                                {
                                    //判断是否有下级列表
                                    SourceURL BBSList = new SourceURL("BBS_List");
                                    string Para = BBSList["para"];
                                    string address = BBSList["address"];
                                    string paras = string.Empty;
                                    string[] arg = new string[] { forum.Attribute("id").Value };
                                    if (!string.IsNullOrEmpty(Para))
                                        paras = string.Format(Para, arg);
                                    string tempaddress = new GetHtmlByUrl().fncGetHtmlByUrl(address + "?" + paras);   //读取列表地址
                                    XElement NoteXml = XElement.Parse(tempaddress);
                                    //var Noteposts = from node in NoteXml.Elements()
                                    //                where node.Name == "posts"
                                    //                select node;
                                    var tempPost = from node in NoteXml.Elements()
                                                   where node.Name == "posts"
                                                   select node;
    
                                    // 判断是否还有下级
                                    if (tempPost != null && tempPost.Count() > 0)
                                        item.Add(new XAttribute("level", "true"));
                                    else
                                        item.Add(new XAttribute("level", "false"));
                                    item.Add(new XElement("id", forum.Attribute("id").Value));
                                }
                                item.Add(new XElement("title", forum.Attribute("name").Value));
                                item.Add(new XElement("date", string.Empty));
                                item.Add(new XElement("img", string.Empty));
                                item.Add(new XElement("summary", string.Empty));
                                list.Add(item);
                            }
                            xRoot.Add(list);
                            return xRoot;
                        }
                    }
                    //  传输的id为子级分类
                    SourceURL _BBSList = new SourceURL("BBS_List");
                    string _Para = _BBSList["para"];
                    string _address = _BBSList["address"];
                    string _paras = string.Empty;
                    string[] _arg = new string[] { id };
                    if (!string.IsNullOrEmpty(_Para))
                        _paras = string.Format(_Para, _arg);
                    string _tempaddress = new GetHtmlByUrl().fncGetHtmlByUrl(_address + "?" + _paras);   //读取列表地址
                    XElement _NoteXml = XElement.Parse(_tempaddress);
                    //var _Noteposts = from node in _NoteXml.Elements()
                    //                 where node.Name == "posts"
                    //                 select node;
                    var _tempPosts = from node in _NoteXml.Elements()
                                     where node.Name == "posts"
                                     select node;
                    if (_tempPosts != null && _tempPosts.Count() > 0)
                    {
                        var _tempPost = _tempPosts.Elements().Where(p => p.Name == "post").Skip(tempInt).Take(tempIntPs);
    
                        if (_tempPost != null && _tempPost.Count() > 0)
                        {
                            foreach (var notePost in _tempPost)
                            {
                                XElement _item = new XElement("item");
                                _item.Add(new XAttribute("level", "false"));
                                _item.Add(new XElement("id", notePost.Attribute("id").Value));
                                _item.Add(new XElement("title", notePost.Element("posttitle").Value.Replace(" ", "").Replace("
    ", "").Replace("
    ", "").Replace("<br />", "")));
                                _item.Add(new XElement("date", string.Empty));
                                _item.Add(new XElement("img", string.Empty));
                                _item.Add(new XElement("summary", notePost.Element("poststatic").Value));
                                list.Add(_item);
                            }
                        }
    
                    }
                    xRoot.Add(list);
                    return xRoot;
                }
            }
            catch (Exception ex)
            {
                LogCommon.ilogDebug.Debug("BBS 拼接列表xml报错:" + ex.ToString());
                return null;
            }
        }
  • 相关阅读:
    Windows删除git本地记住的密码
    C++ fmt库的安装和测试
    仿照struct is_integral 实现struct not_int
    folly库net库udp小例子
    Java阻塞队列BlockingQueue
    正则表达式必知必会摘要
    正则表达式匹配IP地址
    SimpleDateFormat线程安全性测试与解决方案
    Java8 HashMap中哈希桶(table)数组的索引位置
    socket网络编程--基础应用篇
  • 原文地址:https://www.cnblogs.com/WolfBlog/p/4171979.html
Copyright © 2020-2023  润新知