主要用于配置的读取修改等,方便统一化操作,可用于bs或cs代码里面的枚举为文件名,xml文件格式为 id value的格式,使用简单,一看就会
public class XMLSourceHelp { /// <summary> /// XML数据文件数据列表 /// </summary> private static Dictionary<EXMLDataSource,DataTable> XmlDataSourceList = null; /// <summary> /// XML数据文件枚举 /// </summary> public enum EXMLDataSource { Type1 = 1,Type2 = 2 } /// <summary> /// 本类实例对象 /// </summary> private static XMLSourceHelp m_sh; /// <summary> /// XML数据文件数据辅助类 /// </summary> public static XMLSourceHelp SH { get { if (m_sh == null) m_sh = new XMLSourceHelp(); return XMLSourceHelp.m_sh; } private set { XMLSourceHelp.m_sh = value; } } /// <summary> /// 功能描述:获取数据对象 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-10-10 17:47:45 /// 任务编号: /// </summary> /// <param name="file">file</param> /// <returns>返回值</returns> public DataTable GetSource(EXMLDataSource file) { if (XmlDataSourceList == null) return null; DataTable dt = new DataTable(); XmlDataSourceList.TryGetValue(file,out dt); return dt.Copy(); } /// <summary> /// 功能描述:加载数据 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-10-10 17:48:32 /// 任务编号: /// </summary> public void LoadSource() { if (XmlDataSourceList == null) { string strServerPath = AppDomain.CurrentDomain.BaseDirectory; XmlDataSourceList = new Dictionary<EXMLDataSource,DataTable>(); foreach (EXMLDataSource item in Enum.GetValues(typeof(EXMLDataSource))) { XmlDataSourceList.Add(item,LoadXmlInfo(strServerPath + "Data\\" + item + ".xml")); } } } /// <summary> /// 转换ID为中文名 /// </summary> /// <param name="objId">Id数字码</param> /// <param name="file">文件</param> /// <param name="strDefualtValue">默认值</param> /// <returns>返回转换后的名字</returns> public string ConvertIdToName(object objId,EXMLDataSource file,string strDefualtValue = "") { if (objId == null || string.IsNullOrWhiteSpace(objId.ToString())) { return strDefualtValue; } DataTable dtSource = GetSource(file); if (dtSource == null) { return strDefualtValue; } var names = from item in dtSource.AsEnumerable() where item.Field<string>("id") == objId.ToString() select item.Field<string>("value"); if (names == null || names.Count() <= 0) return strDefualtValue; return names.First(); } /// <summary> /// 功能描述:构造方法 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-09-25 09:09:26 /// 任务编号: /// </summary> private XMLSourceHelp() { } /// <summary> /// 加载XML信息 /// </summary> /// <param name="strPath">文件路径</param> /// <returns>返回Xml信息数据表</returns> private DataTable LoadXmlInfo(string strPath) { DataTable dt = CreateDt(); GetSourceByFile(strPath,ref dt); return dt; } /// <summary> /// 功能描述:读取数据 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-09-25 09:28:43 /// 任务编号: /// </summary> /// <param name="strFile">strFile</param> /// <param name="dt">dt</param> private void GetSourceByFile(string strFile,ref DataTable dt) { XmlDocument document = new XmlDocument(); document.Load(strFile); XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes; foreach (XmlNode xn in nodelist) { if (xn.NodeType == XmlNodeType.Element) { DataRow dr = dt.NewRow(); dr[0] = xn.Attributes["id"].Value; dr[1] = xn.Attributes["value"].Value; dt.Rows.Add(dr); } } } /// <summary> /// 功能描述:创建一个DataTable /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-09-25 09:28:08 /// 任务编号: /// </summary> /// <returns>返回值</returns> private DataTable CreateDt() { DataTable dt = new DataTable(); dt.Columns.Add("id",typeof(string)); dt.Columns.Add("value",typeof(string)); return dt; } /// <summary> /// 功能描述:返回指定ID的对应序列号 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-10-12 17:34:40 /// 任务编号: /// </summary> /// <param name="objid">objid</param> /// <param name="EXMLDataSource">文件</param> /// <returns>返回值</returns> public int GetIndexById(object objid,EXMLDataSource file) { DataTable dtSource = GetSource(file); return ZhuoYueE.Dop.Web.Base.ProEnv.GetIndexInTableByField(dtSource,"id",objid.ToString()); } /// <summary> /// 功能描述:返回指定Value的对应的第一个序列号 /// 作 者: 爱给模板网 2gei.cn /// 创建日期:2015-10-12 17:35:50 /// 任务编号: /// </summary> /// <param name="strValue">strValue</param> /// <param name="t">t</param> /// <returns>返回值</returns> public int GetIndexByValue(string strValue,"value",strValue); } /// <summary> /// 修改XML的一个键值 /// </summary> /// <param name="objid">objid</param> /// <param name="strValue">值</param> /// <param name="file">文件</param> public void ModifyValueById(object objid,string strValue,EXMLDataSource file) { string strServerPath = AppDomain.CurrentDomain.BaseDirectory; strServerPath = strServerPath.Substring(0,strServerPath.Length - 1); var names = from item in GetSource(file).AsEnumerable() where item.Field<string>("id") == objid.ToString() select item.Field<string>("value"); if (names != null && names.Count() == 1) { XmlDocument document = new XmlDocument(); document.Load(strServerPath + "\\data\\" + file + ".xml"); XmlNodeList nodelist = document.SelectSingleNode("/source").ChildNodes; foreach (XmlNode xn in nodelist) { if (xn.NodeType == XmlNodeType.Element) { if (xn.Attributes["id"].Value == objid.ToString()) { xn.Attributes["value"].Value = strValue; break; } } } document.Save(strServerPath + "\\data\\" + file + ".xml"); } else { XmlDocument document = new XmlDocument(); document.Load(strServerPath + "\\data\\" + file + ".xml"); XmlNode xn = document.SelectSingleNode("/source"); XmlNode xnNew = document.CreateNode(XmlNodeType.Element,"item",null); XmlAttribute attId = document.CreateAttribute("id"); attId.Value = objid.ToString(); XmlAttribute attvalue = document.CreateAttribute("value"); attvalue.Value = strValue; xnNew.Attributes.Append(attId); xnNew.Attributes.Append(attvalue); xn.AppendChild(xnNew); document.Save(strServerPath + "\\data\\" + file + ".xml"); } if (XmlDataSourceList.ContainsKey(file)) { XmlDataSourceList[file] = LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml"); } else { XmlDataSourceList.Add(file,LoadXmlInfo(strServerPath + "\\Data\\" + file + ".xml")); } } }
原文链接:https://www.f2er.com/xml/294820.html