c#XML配置文件辅助类

时间:2023-11-26 14:11:02

在开发中经常会用到各种kv类型的配置 文件,像这样的

<?xml version="1.0" encoding="utf-8" ?>
<source>
<item id="101" value="1125"/>
<item id="102" value="413"/>
<item id="103" value="181"/>
<item id="104" value="1642"/>
<item id="105" value="926"/>
<item id="106" value="1088"/>
<item id="107" value="1243"/>
</source>

那么我们可以用一个辅助类来很好的解决哦

  public class XMLSourceHelp
{
/// <summary>
/// XML数据文件数据列表
/// </summary>
private static Dictionary<EXMLDataSource, DataTable> XmlDataSourceList = null; /// <summary>
/// XML数据文件枚举
/// </summary>
public enum EXMLDataSource
{
/// <summary>
/// ModuleTypeIconSource文件
/// </summary>
ModuleTypeIconSource = ,
/// <summary>
/// ModuleTypeSource文件
/// </summary>
ModuleTypeSource = ,
/// <summary>
/// SourceTypeFile文件
/// </summary>
SourceTypeFile = ,
/// <summary>
/// FeedBack文件
/// </summary>
FeedBack = ,
/// <summary>
/// QueryBracket文件
/// </summary>
QueryBracket = ,
/// <summary>
/// QueryLogicOperator文件
/// </summary>
QueryLogicOperator = ,
/// <summary>
/// QueryModule文件
/// </summary>
QueryModule = ,
/// <summary>
/// QueryOperator文件
/// </summary>
QueryOperator = ,
/// <summary>
/// QuerySelectModule文件
/// </summary>
QuerySelectModule = ,
/// <summary>
/// 反射参数配置
/// </summary>
ParaConfig=,
/// <summary>
/// 任务类型对应反射类型标记量
/// </summary>
SourceDllIndex = ,
/// <summary>
/// 北京消息队列文件
/// </summary>
Lightweight=
} /// <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>
/// 功能描述:获取数据对象
/// 作  者:huangzh
/// 创建日期: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>
/// 功能描述:加载数据
/// 作  者:huangzh
/// 创建日期: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>
/// 转换数字码为中文名
/// </summary>
/// <param name="objId">Id数字码</param>
/// <param name="item">文件</param>
/// <returns>返回转换后的名字</returns>
public string ConvertIdToName(object objId, EXMLDataSource file)
{
if (objId == null || string.IsNullOrWhiteSpace(objId.ToString()))
{
return string.Empty;
} DataTable dtSource = GetSource(file); if (dtSource == null)
{
return string.Empty;
}
var names = from item in dtSource.AsEnumerable()
where item.Field<string>("id") == objId.ToString()
select item.Field<string>("value");
if (names == null || names.Count() <= )
return "";
return names.First();
} /// <summary>
/// 功能描述:构造方法
/// 作  者:huangzh
/// 创建日期: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>
/// 功能描述:读取数据
/// 作  者:huangzh
/// 创建日期: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[] = xn.Attributes["id"].Value;
dr[] = xn.Attributes["value"].Value;
dt.Rows.Add(dr);
}
}
} /// <summary>
/// 功能描述:创建一个DataTable
/// 作  者:huangzh
/// 创建日期: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的对应序列号
/// 作  者:huangzh
/// 创建日期: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的对应的第一个序列号
/// 作  者:huangzh
/// 创建日期:2015-10-12 17:35:50
/// 任务编号:
/// </summary>
/// <param name="strValue">strValue</param>
/// <param name="t">t</param>
/// <returns>返回值</returns>
public int GetIndexByValue(string strValue, EXMLDataSource file)
{
DataTable dtSource = GetSource(file);
return ZhuoYueE.Dop.Web.Base.ProEnv.GetIndexInTableByField(dtSource, "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(, strServerPath.Length - );
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() == )
{
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"));
}
}
}

其中“EXMLDataSource”就是配置文件名的枚举了。

这个辅助类提供增改查功能,使用方便。只需要这么调用就可以了

XMLSourceHelp.SH.ConvertIdToName("101", XMLSourceHelp.EXMLDataSource.Lightweight);