首页 > 代码库 > linq to xml 操作实例
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 =http://www.mamicode.com/= 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 操作实例
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。