加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

XML的基本操作--

发布时间:2020-12-16 06:16:40 所属栏目:百科 来源:网络整理
导读:XML的增删改查,后台逻辑代码已给出,至于前台数据录入有兴趣可以试试(简单三层)! XML转化为DataTable StudentInfo.xml文件内容: ?xml version="1.0" encoding="GB2312"?students student c_id="1" c_code="2101224101" c_name="One" c_remark="c_remark

XML的增删改查,后台逻辑代码已给出,至于前台数据录入有兴趣可以试试(简单三层)!

XML转化为DataTable

StudentInfo.xml文件内容:

<?xml version="1.0" encoding="GB2312"?>
<students>
  <student c_id="1" c_code="2101224101" c_name="One" c_remark="c_remark" c_type="2010" c_status="0" />
</students>

CmdInfoDAL类:

public class CmdInfoDAL
	{
		public static DbBase db = new DbBase();
		#region XML的基本操作
		/// <summary>
		/// 新增
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="xmlName">StudentInfo</param>
		/// <param name="nodeDir">students/student</param>
		public void AddCmdInfo(CmdInfo entity,string xmlName,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			if (studentNodeList != null)
			{
				int cloumnCounts = studentNodeList[0].Attributes.Count;
				string[] nodeAttributesName = new string[cloumnCounts];
				for (int i = 0; i < cloumnCounts; i++)
				{
					nodeAttributesName[i] = studentNodeList[0].Attributes[i].Name;
				}
				string nodeName = nodeDir.Substring(nodeDir.LastIndexOf("/") + 1);//获取结点名字
				XmlElement xeItem = xmlDoc.CreateElement(nodeName);//创建一个名为nodeName的结点
				xeItem.SetAttributeNode(nodeAttributesName[0],(studentNodeList.Count + 1).ToString());//设置节点的“c_id属性”值,在已有的基础上Counts+1
				//然后绑定entity实例
				root.AppendChild(xeItem);//将节点加上父目录
				xmlDoc.Save(xmlrootDir + "" + xmlName);
			}
		}
		/// <summary>
		///删除XML指定目录下column
		/// </summary>
		/// <param name="macroType"></param>
		/// <param name="xmlName"></param>
		/// <param name="nodeXml"></param>
		/// <returns></returns>
		public int DeleteXmlCmdInfoByType(string Type,string nodeDir)
		{
			int delCounts = 0;
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			if (studentNodeList != null)
			{
				foreach (XmlNode studentNode in studentNodeList)
				{
					if (studentNode.Attributes["c_type"].Value == Type && studentNode.Attributes["c_status"].Value == "0")
					{
						studentNode.Attributes["c_status"].Value = "1";
						delCounts++;
					}
				}
				xmlDoc.Save(xmlrootDir + "" + xmlName);
			}
			return delCounts;
		}
		/// <summary>
		/// 删除指定目录下column的值为“Id”的行
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="xmlName"></param>
		/// <param name="nodeDir"></param>
		/// <returns></returns>
		public int DeleteXmlCmdInfoById(int Id,string nodeDir)
		{
			int delCounts = 0;
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			if (studentNodeList != null)
			{
				foreach (XmlNode studentNode in studentNodeList)
				{
					if (studentNode.Attributes[0].Value == Id.ToString() && studentNode.Attributes["c_status"].Value == "0")
					{
						//假删除
						studentNode.Attributes["c_status"].Value = "1";
						//真删除
						//xmlDoc.RemoveChild(studentNode);
						delCounts++;
					}
				}
				xmlDoc.Save(xmlrootDir + "" + xmlName);
			}
			return delCounts;
		}
		/// <summary>
		/// 修改指定目录下column值为“entity.Id”的行
		/// </summary>
		/// <param name="entity"></param>
		/// <param name="xmlName"></param>
		/// <param name="nodeDir"></param>
		/// <returns></returns>
		public void UpdateXmlCmdInfoById(CmdInfo entity,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			if (studentNodeList != null)
			{
				foreach (XmlNode studentNode in studentNodeList)
				{
					if (studentNode.Attributes[0].Value == entity.c_id.ToString() && studentNode.Attributes["c_status"].Value == "0")
					{
						studentNode.Attributes["c_code"].Value = entity.c_code;
						studentNode.Attributes["c_name"].Value = entity.c_name;
						studentNode.Attributes["c_remark"].Value = entity.c_remark;
						studentNode.Attributes["c_type"].Value = entity.c_type;
						break;//不是循环更新
					}
				}
				xmlDoc.Save(xmlrootDir + "" + xmlName);
			}
		}
		/// <summary>
		/// 获取实例By主键Id
		/// </summary>
		/// <param name="Id"></param>
		/// <param name="xmlName"></param>
		/// <param name="nodeDir"></param>
		/// <returns></returns>
		public CmdInfo GetXmlCmdInfoModel(int Id,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			CmdInfo entity = null;
			if (studentNodeList != null)
			{
				foreach (XmlNode studentNode in studentNodeList)
				{
					if (studentNode.Attributes[0].Value == Id.ToString() && studentNode.Attributes["c_status"].Value == "0")
					{
						entity.c_code = studentNode.Attributes["c_code"].Value;
						entity.c_name = studentNode.Attributes["c_name"].Value;
						entity.c_remark = studentNode.Attributes["c_remark"].Value;
						entity.c_type = studentNode.Attributes["c_type"].Value;
						break;
					}
				}
			}
			return entity;
		}
		/// <summary>
		/// 获取文件名为xmlName的目录为nodeDir的所有数量
		/// </summary>
		/// <param name="xmlName"></param>
		/// <param name="nodeDir"></param>
		/// <returns></returns>
		public int GetXmlCmdInfoCounts(string xmlName,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			int rowCounts = 0;
			if (studentNodeList != null)
			{
				foreach (XmlNode studentNode in studentNodeList)
				{
					rowCounts++;
				}
			}
			return rowCounts;
		}
		/// <summary>
		/// 将Xml指定column值为columnValue的行转化为DataTable
		/// </summary>
		/// <param name="xmlName">指定目录下文件名</param>
		/// <param name="columnName">查询column字段</param>
		/// <param name="columnValue">查询column值满足的值</param>
		/// <param name="nodeDir">在Xml文件内字节路径</param>
		/// <returns></returns>
		public DataTable XmlToDataTable(string xmlName,string columnName,string columnValue,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			DataTable dt = new DataTable();
			if (studentNodeList != null)
			{
				dt.Columns.Clear();
				int columnCounts = studentNodeList[0].Attributes.Count;
				for (int i = 0; i < columnCounts; i++)
				{
					dt.Columns.Add(studentNodeList[0].Attributes[i].Value);
				}
				foreach (XmlNode studentNode in studentNodeList)
				{

					if (studentNode.Attributes[columnName].Value == columnValue && studentNode.Attributes["c_status"].Value == "0")
					{
						DataRow dr =dt.NewRow();
						for (int i = 0; i < columnCounts; i++)
						{
							dr[i] = studentNode.Attributes[i].Value;
						}
						dt.Rows.Add(dr);
					}
				}
			}
			return dt;
		}
		/// <summary>
		/// 将Xml文件转化为DataTable
		/// </summary>
		/// <param name="xmlName"></param>
		/// <param name="nodeDir"></param>
		/// <returns></returns>
		public DataTable XmlToDataTable(string xmlName,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			DataTable dt = new DataTable();
			if (studentNodeList != null)
			{
				dt.Columns.Clear();
				int columnCounts = studentNodeList[0].Attributes.Count;
				for (int i = 0; i < columnCounts; i++)
				{
					dt.Columns.Add(studentNodeList[0].Attributes[i].Value);
				}
				foreach (XmlNode studentNode in studentNodeList)
				{

					if (studentNode.Attributes["c_status"].Value == "0")
					{
						DataRow dr = dt.NewRow();
						for (int i = 0; i < columnCounts; i++)
						{
							dr[i] = studentNode.Attributes[i].Value;
						}
						dt.Rows.Add(dr);
					}
				}
			}
			return dt;
		}
		public DataTable XmlToDataTable(string xmlName,string[] columnValueArry,string nodeDir)
		{
			string xmlrootDir = db.xmlrootDir;
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(xmlrootDir + "" + xmlName);
			//XmlNode root = xmlDoc.SelectSingleNode("students");//查找
			XmlNodeList studentNodeList = xmlDoc.SelectNodes(nodeDir);
			DataTable dt = new DataTable();
			if (studentNodeList != null)
			{
				dt.Columns.Clear();
				int columnCounts = studentNodeList[0].Attributes.Count;
				for (int i = 0; i < columnCounts; i++)
				{
					dt.Columns.Add(studentNodeList[0].Attributes[i].Value);
				}
				foreach (XmlNode studentNode in studentNodeList)
				{
					foreach (string columnValue in columnValueArry)
					{
						if (studentNode.Attributes[columnName].Value == columnValue && studentNode.Attributes["c_status"].Value == "0")
						{
							DataRow dr = dt.NewRow();
							for (int i = 0; i < columnCounts; i++)
							{
								dr[i] = studentNode.Attributes[i].Value;
							}
							dt.Rows.Add(dr);
						}
					}
				}
			}
			return dt;
		}
		#endregion
	}

CmdInfo类:

public class CmdInfo
	{
		public CmdInfo()
		{ }
		private string _c_id;
		private string _c_code;
		private string _c_name;
		private string _c_remark;
		private string _c_type;
		private string _c_status;
		/// <summary>
		/// 
		/// </summary>
		public string c_id
		{
			set { _c_id = value; }
			get { return _c_id; }
		}
		public string c_code
		{
			set { _c_code = value; }
			get { return _c_code; }
		}
		public string c_name
		{
			set { _c_name = value; }
			get { return _c_name; }
		}
		public string c_remark
		{
			set { _c_remark = value; }
			get { return _c_remark; }
		}
		public string c_type
		{
			set { _c_type = value; }
			get { return _c_type; }
		}
		public string c_status
		{
			set { _c_status = value; }
			get { return _c_status; }
		}
	}
DbBase类:
public class DbBase
	{
		public static string xmlrootDirStr = Directory.GetCurrentDirectory();
		public string xmlrootDir
		{
			get {
				string rootDirStr = xmlrootDirStr.ToString();
				rootDirStr = rootDirStr.Substring(0,rootDirStr.LastIndexOf(""));
				rootDirStr = rootDirStr.Substring(0,rootDirStr.LastIndexOf(""));
				return rootDirStr + @"XmlFile";
			}
		}
	}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读