一周代码秀之[11.18~11.24 linq2xml面向对象]
2013-11-21 12:59
323 查看
1、xml
2、提取类
3、linq读取xml,并将其填充实体集合
4、easyui combobox的绑定的默认json格式
5、 linq构建conbobox的json格式
6、前端代码
7、结果
<Sections> <Item key ="1" value ="孕哺期" canBeSelected="false"> <Child key ="6" value ="备孕期"/> <Child key ="7" value ="怀孕期"/> <Child key ="8" value ="分娩期" /> </Item> <Item key ="2" value ="0 - 6月"/> <Item key ="3" value ="7 - 12月"/> <Item key ="4" value ="1 - 3岁"/> <Item key ="5" value ="4 - 6岁"/> </Sections>
2、提取类
public class ParentItem { public int Key { get; set; } public string Value { get; set; } public bool canBeSelected { get; set; } public List<Child> Children { get; set; } } public class Child { public int Key { get; set; } public string Value { get; set; } }
3、linq读取xml,并将其填充实体集合
string menuPath = System.Web.HttpContext.Current.Request.PhysicalApplicationPath + "/Files/Config/BaseConfig.xml"; XDocument doc = XDocument.Load(menuPath); var listP = from x in doc.Descendants("Sections").Elements("Item") select new ParentItem { canBeSelected = false, Key = (int)x.Attribute("key"), Value = (string)x.Attribute("value"), Children = ( from t in x.Descendants("Child") select new Child { Key = (int)t.Attribute("key"), Value = (string)t.Attribute("value") } ).ToList() };
4、easyui combobox的绑定的默认json格式
[{ "id":1, "text":"Folder1", "iconCls":"icon-ok", "children":[{ "id":2, "text":"File1", "checked":true } ]
5、 linq构建conbobox的json格式
var jsonData = from p in listP select new { id = p.Key, text = p.Value, ischecked = selectedNode.Split(',').Contains(p.Key.ToString()) ? true : false, children = from c in p.Children select new { id = c.Key, text = c.Value, ischecked = selectedNode.Split(',').Contains(c.Key.ToString()) ? true : false, } }; JavaScriptSerializer jss = new JavaScriptSerializer(); string firstNode = (selectedNode == "-1" || selectedNode.Trim() == "") ? "[{\"id\":-1,\"checked\":true,\"text\":\"--请选择--\"}," : "[{\"id\":-1,\"text\":\"--请选择--\"},"; if (selectedNode.Trim() == "noheader") { return jss.Serialize(jsonData).Replace("ischecked", "checked"); } return firstNode + jss.Serialize(jsonData).Substring(1).Replace("ischecked", "checked");
6、前端代码
<select class="easyui-combotree" id="txtSection"> </select> $('#txtSection').combotree({ url: "AddEditKnowledges.aspx?action=load", valueField: 'id', textField: 'text', onClick: function (node) { // alert(node.text + ":" + node.id); if (node.text != '孕哺期') { $("#hdnSection").val(node.id); } } });
7、结果
相关文章推荐
- struts_hibernate登陆范例
- Java中split的用法
- C++ 默认成员函数、运算符重载 code
- 关于c++中模板 T 无法限制的替代方案template< T extends Base>
- Eclipse j2ee 插件安装
- python 专题十七 类
- 使用Eclipse平台进行调试
- c#读取Excel的列名问题
- 基本语言细节--《深度探索C++对象模型》--(5)Semantics of Construction,Destruction,and Copy-总结点
- 关于c语言中时间函数的一些归纳
- 关于c语言中时间函数的一些归纳
- gets和puts函数
- 向Python女神推荐这些年我追过的经典书籍
- MapReduce编程实例(三)
- 使用DBUnit集成Spring简化测试
- C语言新学备忘_1
- Mac下安装java开发环境
- 冻结pexcel 代码 -- 这次看看能坚持多久
- Java解析HTML到org.w3c.dom.Document,再把Document输出到文件。
- powerdesigner Could not Initialize JavaVM!