由于最近忙别的事情,所以没来的写文章。先登记一下,稍后时间将介绍该工具的实现和我自己写的
一个通用的xml文件存储数据的机制和代码(任意字符,均可存入,如'<','&'等xml的特殊字符)。
愿与大家分享知识,共同进步。
未来改善目标:
1、如何将多线程的机制,加入到xml文件存储机制。
2、解决不同xml数据表文件中,实现外键链接查询
以下是程序界面和效果
程序可以设置默认显示栏目:
新建大的功能分类和树形深度,没有数目限制。
新建功能栏目
收藏网页,可以添加备注无文字数目限制。
欢迎大家,测试和修改,并且能够发我一份(tiandong19860806@126.com)
核心代码如下:
为了安全起见取消下载,我先重装系统杀毒,之后在把代码打包发上来。
Action.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Collections; namespace UrlMgr { public class Action : BaseClass { #region 常用函数 #region Catalog /// <summary> /// 查询分类方式 /// </summary> /// <returns></returns> public List<Catalog> GetAllCatalog() { DataTable dt = null; List<Catalog> list = null; string[] relations = new string[] { "contains" }; string[] fields = new string[] { MySettings.Catalog_ID }; string[] values = new string[] { null }; string[] types = new string[] { typeof(string).ToString().ToString() }; dt = this.GetXmlRow(MySettings.CatalogXmlFpath, relations, fields, values, types); if (dt != null) { list = new List<Catalog>(); foreach (DataRow dr in dt.Rows) { Catalog item = new Catalog() { ID = dr[MySettings.Catalog_ID].ToString().Trim(), Title = dr[MySettings.Catalog_Title].ToString().Trim(), Desc = dr[MySettings.Catalog_Desc].ToString().Trim(), Creator = dr[MySettings.Catalog_Creator].ToString().Trim(), CreateTime = DateTime.Parse(dr[MySettings.Catalog_CreateTime].ToString().Trim()), Editor = dr[MySettings.Catalog_Editor].ToString().Trim() }; if(dr[MySettings.Catalog_EditTime].ToString().Trim() != "") { item.EditTime = DateTime.Parse(dr[MySettings.Catalog_EditTime].ToString().Trim()); } list.Add(item); } // 升序排序 list.Sort(delegate(Catalog x, Catalog y) { return Comparer<string>.Default.Compare(x.Title, y.Title); }); } dt = null; relations = fields = values = null; return list; } /// <summary> /// 查询分类方式 /// </summary> /// <param name="obj"></param> /// <returns></returns> public List<Catalog> GetCatalog(Catalog obj) { if (obj == null) { obj = new Catalog(); } DataTable dt = null; List<Catalog> list = null; string[] relations = new string[] { "=", "contains", "contains" , "contains" , ">=", "contains" , "<=" }; string[] fields = new string[] { MySettings.Catalog_ID, MySettings.Catalog_Title, MySettings.Catalog_Desc, MySettings.Catalog_Creator, MySettings.Catalog_CreateTime, MySettings.Catalog_Editor, MySettings.Catalog_EditTime }; string[] values = new string[] { obj.ID, obj.Title, obj.Desc, obj.Creator, obj.CreateTime == DateTime.MinValue ? null : obj.CreateTime.ToString(MySettings.TimeLong24HHFormat), obj.Editor, obj.EditTime == DateTime.MinValue ? null : obj.EditTime.ToString(MySettings.TimeLong24HHFormat) }; string[] types = new string[] { typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(DateTime).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(DateTime).ToString().ToLower(), }; dt = this.GetXmlRow(MySettings.CatalogXmlFpath, relations, fields, values, types); if (dt != null) { list = new List<Catalog>(); foreach (DataRow dr in dt.Rows) { Catalog item = new Catalog() { ID = dr[MySettings.Catalog_ID].ToString().Trim(), Title = dr[MySettings.Catalog_Title].ToString().Trim(), Desc = dr[MySettings.Catalog_Desc].ToString().Trim(), Creator = dr[MySettings.Catalog_Creator].ToString().Trim(), CreateTime = DateTime.Parse(dr[MySettings.Catalog_CreateTime].ToString().Trim()), Editor = dr[MySettings.Catalog_Editor].ToString().Trim(), }; if (dr[MySettings.Catalog_EditTime].ToString().Trim() != "") { item.EditTime = DateTime.Parse(dr[MySettings.Catalog_EditTime].ToString().Trim()); } list.Add(item); } // 升序排序 list.Sort(delegate(Catalog x, Catalog y) { return Comparer<string>.Default.Compare(x.Title, y.Title); }); } dt = null; relations = fields = values = null; return list; } /// <summary> /// 添加分类方式 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int AddCatalog(Catalog obj) { int flag = 0; string[] fields = null; List<Dictionary<string, string>> values = null; try { fields = new string[] { MySettings.Catalog_ID, MySettings.Catalog_Title, MySettings.Catalog_Desc, MySettings.Catalog_Creator, MySettings.Catalog_CreateTime, MySettings.Catalog_Editor, MySettings.Catalog_EditTime }; values = new List<Dictionary<string, string>>(); values.Add(new Dictionary<string, string>()); values[0].Add(MySettings.Catalog_ID, obj.ID); values[0].Add(MySettings.Catalog_Title, obj.Title); values[0].Add(MySettings.Catalog_Desc, obj.Desc); values[0].Add(MySettings.Catalog_Creator, obj.Creator); values[0].Add(MySettings.Catalog_CreateTime, obj.CreateTime.ToString(MySettings.TimeLong24HHFormat)); flag = this.NewXmlRow( MySettings.CatalogXmlFpath, fields, values, true ); } catch (Exception ex) { throw ex; } finally { fields = null; values = null; } return flag; } /// <summary> /// 修改分类方式 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int SetCatalog(Catalog obj) { int flag = 0; string[] primaryKeys = null; string[] primaryValues = null; string[] fields = null; string[] values = null; try { primaryKeys = new string[] { MySettings.Catalog_ID }; primaryValues = new string[] { obj.ID }; fields = new string[] { MySettings.Catalog_Title, MySettings.Catalog_Desc, MySettings.Catalog_Editor, MySettings.Catalog_EditTime }; values = new string[] { obj.Title, obj.Desc, obj.Editor, obj.EditTime.ToString(MySettings.TimeLong24HHFormat) }; flag = this.UpdateXmlRow( MySettings.CatalogXmlFpath, primaryKeys, primaryValues, fields, values ); } catch (Exception ex) { throw ex; } finally { primaryKeys = null; primaryValues = null; fields = null; values = null; } return flag; } /// <summary> /// 删除分类方式 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int DelCatalog(Catalog obj) { int flag = 0; string[] primaryKeys = null; string[] primaryValues = null; try { primaryKeys = new string[] { MySettings.Catalog_ID }; primaryValues = new string[] { obj.ID }; flag = this.DeleteXmlRow( MySettings.CatalogXmlFpath, primaryKeys, primaryValues ); } catch (Exception ex) { throw ex; } finally { primaryKeys = null; primaryValues = null; } return flag; } #endregion #region UrlNode /// <summary> /// 查询链接信息 /// </summary> /// <returns></returns> public List<UrlNode> GetAllUrlNode() { DataTable dt = null; List<UrlNode> list = null; string[] relations = new string[] { "contains" }; string[] fields = new string[] { MySettings.UrlNode_ID }; string[] values = new string[] { null }; string[] types = new string[] { typeof(String).ToString().ToLower() }; dt = this.GetXmlRow(MySettings.UrlNodeXmlFpath, relations, fields, values, types); if (dt != null) { list = new List<UrlNode>(); foreach (DataRow dr in dt.Rows) { UrlNode item = new UrlNode() { ID = dr[MySettings.UrlNode_ID].ToString().Trim(), ParentID = dr[MySettings.UrlNode_ParentID].ToString().Trim(), SortCode = dr[MySettings.UrlNode_SortCode].ToString().Trim(), Title = dr[MySettings.UrlNode_Title].ToString().Trim(), Desc = dr[MySettings.UrlNode_Desc].ToString().Trim(), Url = dr[MySettings.UrlNode_Url].ToString().Trim(), Creator = dr[MySettings.UrlNode_Creator].ToString().Trim(), CreateTime = DateTime.Parse(dr[MySettings.UrlNode_CreateTime].ToString().Trim()), Editor = dr[MySettings.UrlNode_Editor].ToString().Trim(), Demo = dr[MySettings.UrlNode_Demo].ToString().Trim() }; if (dr[MySettings.UrlNode_EditTime].ToString().Trim() != "") { item.EditTime = DateTime.Parse(dr[MySettings.UrlNode_EditTime].ToString().Trim()); } list.Add(item); } // 升序排序 list.Sort(delegate(UrlNode x, UrlNode y) { return Comparer<string>.Default.Compare(x.Title, y.Title); }); } dt = null; relations = fields = values = null; return list; } /// <summary> /// 查询链接信息 /// </summary> /// <param name="obj"></param> /// <returns></returns> public List<UrlNode> GetUrlNode(UrlNode obj) { DataTable dt = null; List<UrlNode> list = null; string[] relations = new string[] { "=", "contains", "=", "contains", "contains", "contains" , "contains", ">=", "<=" }; string[] fields = new string[] { MySettings.UrlNode_ID, MySettings.UrlNode_CatalogID, MySettings.UrlNode_ParentID, MySettings.UrlNode_SortCode, MySettings.UrlNode_Title, MySettings.UrlNode_Desc, MySettings.UrlNode_Url, MySettings.Catalog_CreateTime, MySettings.Catalog_CreateTime }; string[] values = new string[] { obj.ID, obj.CatalogID, obj.ParentID, obj.SortCode, obj.Title, obj.Desc, obj.Url, obj.BeginTime.ToString(MySettings.TimeLong24HHFormat), obj.EndTime.ToString(MySettings.TimeLong24HHFormat) }; string[] types = new string[] { typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(String).ToString().ToLower(), typeof(DateTime).ToString().ToLower(), typeof(DateTime).ToString().ToLower(), }; dt = this.GetXmlRow(MySettings.UrlNodeXmlFpath, relations, fields, values, types); if (dt != null) { list = new List<UrlNode>(); foreach (DataRow dr in dt.Rows) { UrlNode item = new UrlNode() { ID = dr[MySettings.UrlNode_ID].ToString().Trim(), CatalogID = dr[MySettings.UrlNode_CatalogID].ToString().Trim(), ParentID = dr[MySettings.UrlNode_ParentID].ToString().Trim(), SortCode = dr[MySettings.UrlNode_SortCode].ToString().Trim(), Title = dr[MySettings.UrlNode_Title].ToString().Trim(), Desc = dr[MySettings.UrlNode_Desc].ToString().Trim(), Url = dr[MySettings.UrlNode_Url].ToString().Trim(), Creator = dr[MySettings.UrlNode_Creator].ToString().Trim(), CreateTime = DateTime.Parse(dr[MySettings.UrlNode_CreateTime].ToString().Trim()), Editor = dr[MySettings.UrlNode_Editor].ToString().Trim(), Demo = dr[MySettings.UrlNode_Demo].ToString().Trim() }; if (dr[MySettings.UrlNode_EditTime].ToString().Trim() != "") { item.EditTime = DateTime.Parse(dr[MySettings.UrlNode_EditTime].ToString().Trim()); } list.Add(item); } // 升序排序 list.Sort(delegate(UrlNode x, UrlNode y) { return Comparer<string>.Default.Compare(x.ParentID, y.ParentID); }); } dt = null; relations = fields = values = null; return list; } /// <summary> /// 添加链接信息 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int AddUrlNode(UrlNode obj) { int flag = 0; string[] fields = null; List<Dictionary<string, string>> values = null; try { fields = new string[] { MySettings.UrlNode_ID, MySettings.UrlNode_CatalogID, MySettings.UrlNode_ParentID, MySettings.UrlNode_SortCode, MySettings.UrlNode_Title, MySettings.UrlNode_Desc, MySettings.UrlNode_Url, MySettings.UrlNode_Creator, MySettings.UrlNode_CreateTime, MySettings.UrlNode_Editor, MySettings.UrlNode_EditTime, MySettings.UrlNode_Demo }; values = new List<Dictionary<string, string>>(); values.Add(new Dictionary<string, string>()); values[0].Add(MySettings.UrlNode_ID, obj.ID); values[0].Add(MySettings.UrlNode_CatalogID, obj.CatalogID); values[0].Add(MySettings.UrlNode_ParentID, obj.ParentID); values[0].Add(MySettings.UrlNode_SortCode, obj.SortCode); values[0].Add(MySettings.UrlNode_Title, obj.Title); values[0].Add(MySettings.UrlNode_Desc, obj.Desc); values[0].Add(MySettings.UrlNode_Url, obj.Url); values[0].Add(MySettings.UrlNode_Creator, obj.Creator); values[0].Add(MySettings.UrlNode_CreateTime, obj.CreateTime.ToString(MySettings.TimeLong24HHFormat)); values[0].Add(MySettings.UrlNode_Demo, obj.Demo); flag = this.NewXmlRow( MySettings.UrlNodeXmlFpath, fields, values, true ); } catch (Exception ex) { throw ex; } finally { fields = null; values = null; } return flag; } /// <summary> /// 修改链接信息 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int SetUrlNode(UrlNode obj) { int flag = 0; string[] primaryKeys = null; string[] primaryValues = null; string[] fields = null; string[] values = null; try { primaryKeys = new string[] { MySettings.UrlNode_ID }; primaryValues = new string[] { obj.ID }; fields = new string[] { MySettings.UrlNode_ParentID, MySettings.UrlNode_SortCode, MySettings.UrlNode_Title, MySettings.UrlNode_Desc, MySettings.UrlNode_Url, MySettings.UrlNode_Editor, MySettings.UrlNode_EditTime, MySettings.UrlNode_Demo }; values = new string[] { obj.ParentID, obj.SortCode, obj.Title, obj.Desc, obj.Url, obj.Editor, obj.EditTime.ToString(MySettings.TimeLong24HHFormat), obj.Demo }; flag = this.UpdateXmlRow( MySettings.UrlNodeXmlFpath, primaryKeys, primaryValues, fields, values ); } catch (Exception ex) { throw ex; } finally { primaryKeys = null; primaryValues = null; fields = null; values = null; } return flag; } /// <summary> /// 删除链接信息 /// </summary> /// <param name="obj"></param> /// <returns></returns> public int DelUrlNode(UrlNode obj) { int flag = 0; string[] primaryKeys = null; string[] primaryValues = null; try { primaryKeys = new string[] { MySettings.UrlNode_ID }; primaryValues = new string[] { obj.ID }; flag = this.DeleteXmlRow( MySettings.UrlNodeXmlFpath, primaryKeys, primaryValues ); } catch (Exception ex) { throw ex; } finally { primaryKeys = null; primaryValues = null; } return flag; } #endregion #endregion #region xml数据管理 /// <summary> /// 新建xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <param name="isCreateNew"></param> /// <returns></returns> public int NewXmlRow( string fpath, string[] fields, List<Dictionary<string, string>> values, bool isCreateNew ) { XmlHelper xmlHelper = null; int result = 0; try { xmlHelper = new XmlHelper(); result = xmlHelper.NewXmlRow(fpath, fields, values, isCreateNew); } catch (Exception ex) { throw ex; } finally { xmlHelper.Dispose(); } return result; } /// <summary> /// 删除xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="primaryKeys"></param> /// <param name="primaryValues"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public int DeleteXmlRow( string fpath, string[] primaryKeys, string[] primaryValues ) { XmlHelper xmlHelper = null; int result = 0; try { xmlHelper = new XmlHelper(); result = xmlHelper.DeleteXmlRow(fpath, primaryKeys, primaryValues); } catch (Exception ex) { throw ex; } finally { xmlHelper.Dispose(); } return result; } /// <summary> /// 修改xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="primaryKeys"></param> /// <param name="primaryValues"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public int UpdateXmlRow( string fpath, string[] primaryKeys, string[] primaryValues, string[] fields, string[] values) { XmlHelper xmlHelper = null; int result = 0; try { xmlHelper = new XmlHelper(); result = xmlHelper.UpdateXmlRow(fpath, primaryKeys, primaryValues, fields, values); } catch (Exception ex) { throw ex; } finally { xmlHelper.Dispose(); } return result; } /// <summary> /// 查询xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="relation"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public DataTable GetXmlRow(string fpath, string[] relation, string[] fields, string[] values, string[] types) { XmlHelper xmlHelper = null; DataTable result = null; try { xmlHelper = new XmlHelper(); result = xmlHelper.GetXmlRow(fpath, relation, fields, values, types); } catch (Exception ex) { throw ex; } finally { xmlHelper.Dispose(); } return result; } #endregion } public class XmlHelper : BaseClass { public bool Exsist(string fpath) { if(System.IO.File.Exists(fpath)) { return true; } return false; } public bool CreatXmlDoc(string fpath, string[] keys) { if (this.Exsist(fpath)) { throw new Exception(string.Format("{0} is exsist!", fpath)); } System.IO.File.Create(fpath).Dispose(); return true; } /// <summary> /// 新建若干xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <param name="isCreateNew"></param> /// <returns></returns> public int NewXmlRow( string fpath, string[] fields, List<Dictionary<string,string>> values , bool isCreateNew ) { int result = 0; System.Xml.XmlDocument xdoc = null; System.Xml.XmlNode xFields = null; System.Xml.XmlNode xDataInfo = null; System.Xml.XmlNode xTmpNode = null; System.Xml.XmlNode xTmpSubNode = null; List<string> currentKey = null; string[] keys = null; string xml = ""; string tmp = ""; if (fields == null && fields.Length == 0) { throw new Exception("fields is null or empty"); } if (values == null && values.Count == 0) { throw new Exception("values is null or empty"); } try { EncodeXmlFields(ref fields); EncodeXmlFields(ref values); xdoc = new System.Xml.XmlDocument(); try { xdoc.Load(fpath); } catch (Exception ex) { if (isCreateNew) { System.IO.File.Create(fpath).Dispose(); } else { throw new Exception("XmlDocument is not exsist!"); } } if (xdoc == null || xdoc.ChildNodes == null || xdoc.ChildNodes.Count == 0 || xdoc.ChildNodes[0].ChildNodes == null || xdoc.ChildNodes[0].ChildNodes.Count != 2 || xdoc.ChildNodes[0].ChildNodes[0].Name != "ColNames" || xdoc.ChildNodes[0].ChildNodes[1].Name != "DataInfo" ) { // fpath的Xml文档,不存在或不正确,需要重建 if (isCreateNew) { // Init ColNames Node foreach (string field in fields) { tmp += string.Format("<Row><![CDATA[{0}]]></Row>", field); } xml += string.Format("<ColNames>{0}</ColNames>", tmp); // Init DataInfo Node tmp = "<DataInfo></DataInfo>"; xml += tmp ; // Init XmlDoc xml = string.Format("<XmlDoc>{0}</XmlDoc>", xml); xdoc.InnerXml = xml; xdoc.Save(fpath); } else { throw new Exception("XmlDocument is error format!"); } } // 重新打开Xml文档,并且写入DataInfo记录 xdoc.Load(fpath); xFields = xdoc.ChildNodes[0].ChildNodes[0]; // 判断Xml文件中的栏位和录入数据的栏位是否相同 currentKey = new List<string>(); foreach (System.Xml.XmlNode node in xFields.ChildNodes) { currentKey.Add(node.InnerText); } if (fields.Length > currentKey.Count) { throw new Exception("Parameter fields lent is bigger than XmlDocument ColNames define!"); } foreach (string s in fields) { if (!currentKey.Contains(s)) { throw new Exception(String.Format("XmlDocument ColNames don't contains colname = '{0}'!", s)); } } xDataInfo = xdoc.ChildNodes[0].ChildNodes[1]; foreach (Dictionary<string, string> list in values) { // DataInfo节点每新增一笔数据记录 keys = list.Keys.ToArray<string>(); if (list != null && list.Count > 0) { xTmpNode = xdoc.CreateElement("Row"); foreach (string key in keys) { xTmpSubNode = xdoc.CreateElement(key); xTmpSubNode.InnerXml = string.Format("<![CDATA[{0}]]>", list[key]); xTmpNode.AppendChild(xTmpSubNode); } if (xTmpNode.ChildNodes != null && xTmpNode.ChildNodes.Count > 0) { xDataInfo.AppendChild(xTmpNode); } } } // xdoc.Save(fpath); result = values.Count; } catch (Exception ex) { throw ex; } finally { xdoc = null; xFields = null; xDataInfo = null; xTmpNode = null; xTmpSubNode = null; currentKey = null; keys = null; xml = null; tmp = null; } return result; } /// <summary> /// 删除xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="primaryKeys"></param> /// <param name="primaryValues"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public int DeleteXmlRow( string fpath, string[] primaryKeys, string[] primaryValues ) { int result = 0; System.Xml.XmlDocument xdoc = null; System.Xml.XmlNode xFields = null; System.Xml.XmlNode xDataInfo = null; List<string> currentKey = null; if (primaryKeys == null && primaryKeys.Length == 0) { throw new Exception("fields is null or empty"); } if (primaryValues == null && primaryValues.Length == 0) { throw new Exception("values is null or empty"); } try { EncodeXmlFields(ref primaryKeys); EncodeXmlFields(ref primaryValues); xdoc = new System.Xml.XmlDocument(); xdoc.Load(fpath); if (xdoc == null || xdoc.ChildNodes == null || xdoc.ChildNodes.Count == 0 || xdoc.ChildNodes[0].ChildNodes == null || xdoc.ChildNodes[0].ChildNodes.Count != 2 || xdoc.ChildNodes[0].ChildNodes[0].Name != "ColNames" || xdoc.ChildNodes[0].ChildNodes[1].Name != "DataInfo" ) { // fpath的Xml文档,不存在或不正确,需要重建 throw new Exception("XmlDocument is error format"); } // 删除DataInfo某些记录 xFields = xdoc.ChildNodes[0].ChildNodes[0]; // 判断Xml文件中的栏位和录入数据的栏位是否相同 currentKey = new List<string>(); foreach (System.Xml.XmlNode node in xFields.ChildNodes) { currentKey.Add(node.InnerText); } if (primaryKeys.Length > currentKey.Count) { throw new Exception("Parameter primaryKeys lent is bigger than XmlDocument ColNames define!"); } foreach (string s in primaryKeys) { if (!currentKey.Contains(s)) { throw new Exception(String.Format("XmlDocument ColNames don't contains colname = '{0}'!", s)); } } xDataInfo = xdoc.ChildNodes[0].ChildNodes[1]; string filter = ""; for (int i = 0; i < primaryKeys.Length; i++) { filter += string.Format("{0} {1} '{2}'and ", primaryKeys[i], "=", primaryValues[i]); } if(filter != "" && filter.Length > 4) { filter = filter.Substring(0, filter.Length -4); } string select = string.Format("descendant::{0}[{1}]", "Row", filter); System.Xml.XmlNodeList nodelist = xDataInfo.SelectNodes(select); if (nodelist != null && nodelist.Count == 1) { for (int i = 0; i < nodelist.Count; i++) { xDataInfo.RemoveChild(nodelist[i]); } // xdoc.Save(fpath); result = 1; } } catch (Exception ex) { throw ex; } finally { xdoc = null; xFields = null; xDataInfo = null; currentKey = null; } return result; } /// <summary> /// 修改xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="primaryKeys"></param> /// <param name="primaryValues"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public int UpdateXmlRow( string fpath, string[] primaryKeys, string[] primaryValues, string[] fields, string[] values) { int result = 0; System.Xml.XmlDocument xdoc = null; System.Xml.XmlNode xFields = null; System.Xml.XmlNode xDataInfo = null; System.Xml.XmlNodeList nodelist = null; List<string> currentKey = null; if (fields == null && fields.Length == 0) { throw new Exception("fields is null or empty"); } if (values == null && values.Length == 0) { throw new Exception("values is null or empty"); } try { EncodeXmlFields(ref primaryKeys); EncodeXmlFields(ref primaryValues); EncodeXmlFields(ref fields); EncodeXmlFields(ref values); xdoc = new System.Xml.XmlDocument(); xdoc.Load(fpath); if (xdoc == null || xdoc.ChildNodes == null || xdoc.ChildNodes.Count == 0 || xdoc.ChildNodes[0].ChildNodes == null || xdoc.ChildNodes[0].ChildNodes.Count != 2 || xdoc.ChildNodes[0].ChildNodes[0].Name != "ColNames" || xdoc.ChildNodes[0].ChildNodes[1].Name != "DataInfo" ) { // fpath的Xml文档,不存在或不正确,需要重建 throw new Exception("XmlDocument is error format!"); } // 删除DataInfo某些记录 xFields = xdoc.ChildNodes[0].ChildNodes[0]; // 判断Xml文件中的栏位和录入数据的栏位是否相同 currentKey = new List<string>(); foreach (System.Xml.XmlNode node in xFields.ChildNodes) { currentKey.Add(node.InnerText); } if (fields.Length > currentKey.Count) { throw new Exception("Parameter fields lent is bigger than XmlDocument ColNames define!"); } foreach (string s in fields) { if (!currentKey.Contains(s)) { throw new Exception(String.Format("XmlDocument ColNames don't contains colname = '{0}'!", s)); } } xDataInfo = xdoc.ChildNodes[0].ChildNodes[1]; string filter = ""; for (int i = 0; i < primaryKeys.Length; i++) { filter += string.Format("{0} {1} '{2}'and ", primaryKeys[i], "=", primaryValues[i]); } if (filter != "" && filter.Length > 4) { filter = filter.Substring(0, filter.Length - 4); } string select = string.Format("descendant::{0}[{1}]", "Row", filter); nodelist = xDataInfo.SelectNodes(select); if (nodelist != null && nodelist.Count == 1) { //for (int i = 0; i < fields.Length; i++) //{ // nodelist[0].SelectSingleNode(string.Format("descendant::{0}", fields[i])).InnerXml = values[i]; //} for (int i = 0; i < nodelist.Count; i++) { for (int j = 0; j < fields.Length; j++) { if (nodelist[0].SelectSingleNode(string.Format("descendant::{0}", fields[j])) == null) { System.Xml.XmlElement newNode = xdoc.CreateElement(fields[j]); newNode.InnerXml = string.Format("<![CDATA[{0}]]>", values[j]); nodelist[0].AppendChild(newNode); } else { nodelist[0].SelectSingleNode(string.Format("descendant::{0}", fields[j])).InnerXml = values[j]; } } } xdoc.Save(fpath); result = 1; } else { result = 0; } } catch (Exception ex) { throw ex; } finally { xdoc = null; xFields = null; xDataInfo = null; nodelist = null; currentKey = null; } return result; } /// <summary> /// 查询xml记录 /// </summary> /// <param name="fpath"></param> /// <param name="relation"></param> /// <param name="fields"></param> /// <param name="values"></param> /// <returns></returns> public DataTable GetXmlRow(string fpath, string[] relation, string[] fields, string[] values, string[] types) { DataTable result = null; System.Xml.XmlDocument xdoc = null; System.Xml.XmlNode xFields = null; System.Xml.XmlNode xDataInfo = null; System.Xml.XmlNodeList nodelist = null; List<string> currentKey = null; if (fields == null && fields.Length == 0) { throw new Exception("fields is null or empty"); } if (values == null && values.Length == 0) { throw new Exception("values is null or empty"); } try { EncodeXmlFields(ref fields); EncodeXmlFields(ref values); if (!System.IO.File.Exists(fpath)) { throw new System.IO.FileNotFoundException(string.Format("{0} is not exsist!", fpath)); } xdoc = new System.Xml.XmlDocument(); xdoc.Load(fpath); if (xdoc == null || xdoc.ChildNodes == null || xdoc.ChildNodes.Count == 0 || xdoc.ChildNodes[0].ChildNodes == null || xdoc.ChildNodes[0].ChildNodes.Count != 2 || xdoc.ChildNodes[0].ChildNodes[0].Name != "ColNames" || xdoc.ChildNodes[0].ChildNodes[1].Name != "DataInfo" ) { // fpath的Xml文档,不存在或不正确,需要重建 throw new Exception("XmlDocument is error format!"); } // 删除DataInfo某些记录 xFields = xdoc.ChildNodes[0].ChildNodes[0]; // 判断Xml文件中的栏位和录入数据的栏位是否相同 currentKey = new List<string>(); foreach (System.Xml.XmlNode node in xFields.ChildNodes) { currentKey.Add(node.InnerText); } #region 2011/08/14 田东 修改原因:增加对时间栏位查询的功能,所以注释该部分代码 //if (fields.Length > currentKey.Count) //{ // throw new Exception("Parameter fields lent is bigger than XmlDocument ColNames define!"); //} #endregion foreach (string s in fields) { if (!currentKey.Contains(s)) { throw new Exception(String.Format("XmlDocument ColNames don't contains colname = '{0}'!", s)); } } xDataInfo = xdoc.ChildNodes[0].ChildNodes[1]; string filter = ""; for (int i = 0; i < fields.Length; i++) { if (values[i] == null) { continue; } if (values[i] == System.Xml.XmlConvert.EncodeName("0001-01-01 00:00:00") && types[i] == typeof(DateTime).ToString().ToLower()) { continue; } switch (relation[i].ToLower()) { case "=": case "eq": case "ieq": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case "!=": case "ne": case "ine": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case "<": case "lt": case "ilt": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case "<=": case "le": case "ile": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case ">": case "gt": case "igt": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case ">=": case "ge": case "ige": { filter += string.Format("{0} {1} '{2}' and ", fields[i], relation[i], values[i]); break; } case "contains": { filter += string.Format("contains({0} ,'{1}') and ", fields[i], values[i]); break; } } } if (filter != "" && filter.Length > 4) { filter = filter.Trim().Substring(0, filter.Length - 4); } if (filter.Length == 0) {// 当一个查询条件都不存在 filter = string.Format("contains({0} ,'{1}')", fields[0], ""); } //string select = string.Format("descendant::{0}[{1}]", "Row", filter); string select = string.Format("//XmlDoc/DataInfo/Row[{0}]",filter); nodelist = xDataInfo.SelectNodes(select); if (nodelist != null && nodelist.Count > 0) { result = new DataTable(); foreach (System.Xml.XmlNode item in xFields.ChildNodes) { result.Columns.Add(item.InnerText, typeof(string)); } foreach (System.Xml.XmlNode item in nodelist) { DataRow row = result.NewRow(); if (item.ChildNodes != null && item.ChildNodes.Count > 0) { foreach (System.Xml.XmlNode i in item.ChildNodes) { row[i.Name] = System.Xml.XmlConvert.DecodeName(i.InnerText); } } result.Rows.Add(row); } } } catch (Exception ex) { throw ex; } finally { xdoc = null; xFields = null; xDataInfo = null; nodelist = null; currentKey = null; } return result; } /// <summary> /// 将字段名称进行编码 /// </summary> /// <param name="arr"></param> public static void EncodeXmlFields(ref string[] arr) { if (arr != null && arr.Length > 0) { for (int i = 0; i < arr.Length; i++) { arr[i] = System.Xml.XmlConvert.EncodeName(arr[i]); } } } public static void EncodeXmlFields(ref List<Dictionary<string, string>> list) { if (list != null && list.Count > 0) { for(int i = 0; i < list.Count;i++) { string[] keys = list[i].Keys.ToArray<string>(); foreach(string key in keys) { list[i][key] = System.Xml.XmlConvert.EncodeName(list[i][key]); } } } } } public class Common : BaseClass { } }