码迷,mamicode.com
首页 > 其他好文 > 详细

linq to xml 操作实例

时间:2014-12-18 16:54:00      阅读:139      评论:0      收藏:0      [点我收藏+]

标签:style   blog   ar   io   color   os   sp   for   on   

/// <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("\n", "").Replace("\r", "").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;
        }
    }

 

linq to xml 操作实例

标签:style   blog   ar   io   color   os   sp   for   on   

原文地址:http://www.cnblogs.com/WolfBlog/p/4171979.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!