一周代码秀之[11.18~11.24 linq2xml面向对象]

1、xml

  <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、结果

posted @ 2013-11-21 12:59  哈哈2222  阅读(471)  评论(3编辑  收藏  举报