跑赢自己

导航

 

由于第一次写博客,写的不好的地方,还请各位大神多多指点,

讲解一下:xml动态插入数据并保存,写这个时候费了我不少劲,最后终于皇天不负有心人让我搞出来了,特意分享给大家,写的不完美的地方还请大家多多指点

数据库表结构

Categoryid       GUid自动生成

CategoryName  分类名称
CategoryIdentifies 分类标示
ParentIdentifies 父标示

Sort 排序

控制器代码:

  public ActionResult CreateCategory(CategoryForm model)
        {

            //定义并从xml文件中加载节点(根节点)

            XElement rootNode = XElement.Load(HttpRuntime.AppDomainAppPath + "Runtime/Category.xml");
            IEnumerable<XElement> ele = rootNode.Elements("Controller").Elements("Root");
            XElement dnode = rootNode.Elements("Controller").FirstOrDefault();
            XElement pElement = null;
            //判断xml中是否有root节点

            var count = 0;
            XElement Parentnode = ele.Where(e => ((string)e.Attribute("CategoryName")) == model.CategoryName).FirstOrDefault();
            var CategoryIdentifies = 0.0;
            //判断当前节点是否存在
            if (Parentnode == null)
            {
                //判断是否是父节点
                if (model.ParentIdentifies == "0")
                {   //获取同级父节点的个数
                    count = ele.Count();
                    if (count > 0)
                    {
                        CategoryIdentifies = Convert.ToDouble(model.CategoryIdentifies) + count;
                        pElement = new XElement("Root", new XAttribute("CategoryName", model.CategoryName), new XAttribute("CategoryIdentifies", CategoryIdentifies), new XAttribute("ParentIdentifies", "0"), new XAttribute("sort", model.Sort));
                        dnode.Add(pElement);
                      
                    }
                    else
                    {
                        pElement = new XElement("Root", new XAttribute("CategoryName", model.CategoryName), new XAttribute("CategoryIdentifies", model.CategoryIdentifies), new XAttribute("ParentIdentifies", "0"), new XAttribute("sort", model.Sort));
                        dnode.Add(pElement);
                       
                    }
                }
                else
                {
                    //获取同级几点的个数
                    count = rootNode.Descendants("Root").Where(e => ((string)e.Attribute("ParentIdentifies")) == model.ParentIdentifies).Count();
                    //获取父节点
                    var sElement = rootNode.Descendants("Root").Where(e => ((string)e.Attribute("CategoryIdentifies")) == model.ParentIdentifies).FirstOrDefault();
                    if (count == 0)
                    {
                        pElement = new XElement("Root", new XAttribute("CategoryName", model.CategoryName), new XAttribute("CategoryIdentifies", sElement.Attribute("CategoryIdentifies").Value + model.ParentIdentifies), new XAttribute("ParentIdentifies", model.ParentIdentifies), new XAttribute("sort", model.Sort));
                        sElement.Add(pElement);
                       

                    }
                    else
                    {
                        CategoryIdentifies = Convert.ToDouble(sElement.Attribute("CategoryIdentifies").Value + model.ParentIdentifies) + count;
                        pElement = new XElement("Root", new XAttribute("CategoryName", model.CategoryName), new XAttribute("CategoryIdentifies", CategoryIdentifies), new XAttribute("ParentIdentifies", model.ParentIdentifies), new XAttribute("sort", model.Sort));
                        sElement.Add(pElement);
                       
                    }
                }
            }
            var id = model.CategoryId = Guid.NewGuid();//生成一个GUID
            //保存对xml的更改操作
            rootNode.Save(HttpRuntime.AppDomainAppPath + "Runtime/Category.xml");
            return RedirectToAction("CategoryList");
        }

view视图这块大家可以自己写,按照自己的喜好,也可以写成树,

xml结果:

注意规则,这个规则是进行查询用的

<?xml version="1.0" encoding="utf-8"?>
<Category>
  <Controller name="AdminPublish">
    <Root CategoryName="etre" CategoryIdentifies="100" ParentIdentifies="0" sort="1">
      <Root CategoryName="wet" CategoryIdentifies="100100" ParentIdentifies="100" sort="1">
        <Root CategoryName="wetsryerset" CategoryIdentifies="100100100100" ParentIdentifies="100100" sort="1" />
      </Root>
      <Root CategoryName="wetsryer" CategoryIdentifies="100101" ParentIdentifies="100" sort="1" />
      <Root CategoryName="wetsryersetewt" CategoryIdentifies="100102" ParentIdentifies="100" sort="1" />
    </Root>
  </Controller>
</Category>

 

我实现的功能是一个产品分类(联动)查询


 

注意这是联动,这个联动是根据 ParentIdentifies这个字段进行查询的,注意查看数据库表结构规则,和xml规则,有人可能问了,为什么不直接操作数据库,却操作xml,原因很简单,减少数据库压力,在这里不一一讲述了,希望能帮到大家,谢谢

 

posted on 2016-12-15 11:06  跑赢自己  阅读(551)  评论(3编辑  收藏  举报