XML 动态 插入 父类标示 子类标示
由于第一次写博客,写的不好的地方,还请各位大神多多指点,
讲解一下: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 = ;
XElement Parentnode = ele.Where(e => ((string)e.Attribute("CategoryName")) == model.CategoryName).FirstOrDefault();
var CategoryIdentifies = 0.0;
//判断当前节点是否存在
if (Parentnode == null)
{
//判断是否是父节点
if (model.ParentIdentifies == "")
{ //获取同级父节点的个数
count = ele.Count();
if (count > )
{
CategoryIdentifies = Convert.ToDouble(model.CategoryIdentifies) + count;
pElement = new XElement("Root", new XAttribute("CategoryName", model.CategoryName), new XAttribute("CategoryIdentifies", CategoryIdentifies), new XAttribute("ParentIdentifies", ""), 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", ""), 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 == )
{
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,原因很简单,减少数据库压力,在这里不一一讲述了,希望能帮到大家,谢谢
XML 动态 插入 父类标示 子类标示的更多相关文章
- iOS_动态插入或删除行
终于效果图: 分MVC三层设计;自己定义的Cell有两种;一种是MainCell,由ModelArr提供数据源;还有一种是插入的cell,由代码创建,而且由另外一个数组供状态数据 数据源部分: wat ...
- Java实现PDF和Excel生成和数据动态插入以及导出
一.序言 Excel.PDF的导出.导入是我们工作中经常遇到的一个问题,刚好今天公司业务遇到了这个问题,顺便记个笔记以防下次遇到相同的问题而束手无策. 公司有这么两个需求: 需求一.给了一个表单,让把 ...
- Java特性之多态父类与子类之间的调用
问题描述: Java三大特性,封装.继承.多态,一直没搞懂其中多态是什么,最近研究了一下,关于父类和子类之间的调用.下面是一个测试类,源代码如下: package com.test; public c ...
- JavaScript 动态插入 CSS
写组件时有时想把一些组件特性相关的 CSS 样式封装在 JS 里,这样更内聚,改起来方便.JS 动态插入 CSS 两个步骤就可以 创建一个 style 对象 使用 stylesheet 的 inser ...
- java中父类与子类, 不同的两个类中的因为构造函数由于递归调用导致栈溢出问题
/* 对于类中对成员变量的初始化和代码块中的代码全部都挪到了构造函数中, 并且是按照java源文件的初始化顺序依次对成员变量进行初始化的,而原构造函数中的代码则移到了构造函数的最后执行 */ impo ...
- FMDB处理动态插入语句
昨天做一个需求,参数的数量不确定,所以无法使用这个API: - (BOOL)executeUpdate:(NSString*)sql, ... 但是用 - (BOOL)executeUpdate:(N ...
- Sql [hierarchyid]类型如何动态插入层级数据
[hierarchyid] 是个不错的数据类型,能够方便的操作树型结构,网上找了很多资料没找到如何做到动态插入节点的例子,只好从MSDN认真看了下资料写出了一个DEMO CREATE TABLE Em ...
- java父类转子类的一个方法
一般子类可以转父类.但父类转子类就会报cast error. 使用jsonobject 思想:先把父类转jsonstring 再把jsonstring转子类.剩余的子类值可以设定进去. import ...
- P188 实战练习(父类和子类)
1.创建一个父类,在父类中创建两个方法,在子类中覆盖第二个方法,为子类创建一个对象,将它向上转型到基类并调用这个方法. 创建Computer父类: package org.hanqi.practise ...
随机推荐
- .net core 学习
资源: https://github.com/aspnet/home https://github.com/dotnet/cli
- eventbus 备注
Event在整个系统中是单例的. EventBus.getDefault().register(this); 注册 EventBus.getDefault().unregister(this); 注销 ...
- linux下使用adb连接android手机
一.新建文件 cat /etc/udev/rules.d/51-android.rules SUBSYSTEM==" 二.重启 udev sudo /etc/init.d/udev rest ...
- Java static 的一两点使用
这篇文章是关于Java static关键字的使用,主要会介绍以下的内容: static 的概念 static的各种应用 总结 static 是什么 static 顾名思义是静态的意思.与this相对, ...
- iOS-多线程基础
进程与线程: 1> 一个应用程序对应一个进程,一个进程帮助程序占据一块存储空间 2> 要想在进程中执行任务,就必须开启线程,一条线程就代表一个任务 3> 一个进程中允许开 ...
- python发送邮件及附件
今天给大伙说说python发送邮件,官方的多余的话自己去百度好了,还有一大堆文档说实话不到万不得已的时候一般人都不会去看,回归主题: 本人是mac如果没有按照依赖模块的请按照下面的截图安装 导入模块如 ...
- [转]Linux常用命令
系统信息arch 显示机器的处理器架构(1) uname -m 显示机器的处理器架构(2) uname -r 显示正在使用的内核版本 dmidecode -q 显示硬件系统部件 - (SMBIOS / ...
- word-spacing汉字不起作用的解决方法
异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 调节文字间的距离,发现==>word-spacing汉字不起作用 研究发现, ...
- Sql Server系列:Insert语句
1 INSERT语法 [ WITH <common_table_expression> [ ,...n ] ] INSERT { [ TOP ( expression ) [ PERCEN ...
- Android之startService()和bindService()区别
1. 生命周期: startService()方式启动,Service是通过接受Intent并且会经历onCreate()和onStart().当用户在发出意图使之销毁时会经历onDestroy(), ...