XML实例
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<systemINFO>
<MENUGROUPMAIN>
<T_TMENUGROUPMAIN F_MENUGROUPCODE="1" F_MENUGROUPNAME="123"/>
<T_TMENUGROUPMAIN F_MENUGROUPCODE="6" F_MENUGROUPNAME="666"/>
<T_TMENUGROUPMAIN F_MENUGROUPCODE="9" F_MENUGROUPNAME="999"/>
</MENUGROUPMAIN>
</systemINFO>
</configuration>
//查询
public Dictionary<int, string> GetConfigTableSetting(string tableName, string columnCode, string columnName)
{
string configPath;
Dictionary<int, string> dictionary;
XmlDocument xmlDocument;
XmlNodeList xmlNodeList;
XmlAttribute xmlAttrubuteCode;
XmlAttribute xmlAttrubuteName;
xmlDocument = new XmlDocument();
dictionary = new Dictionary<int, string>();
try
{
configPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
xmlDocument.Load(configPath);
xmlNodeList = xmlDocument.GetElementsByTagName(tableName);
for (int i = 0; i < xmlNodeList.Count; i++)
{
xmlAttrubuteCode = xmlNodeList[i].Attributes[columnCode];
xmlAttrubuteName = xmlNodeList[i].Attributes[columnName];
dictionary.Add(Convert.ToInt32(xmlAttrubuteCode.Value), xmlAttrubuteName.Value);
}
}
catch
{
}
return dictionary;
}
//添加
public bool InsertConfigSettings(string nodeName,string tableName, string columnCode,string columnName,string code,string name)
{
XmlDocument xmlDocument;//xml文件
XmlElement rootNode;//根节点
XmlNode subNode;//子节点
XmlNode secondarySubNode;//次级子节点
XmlElement xmlElement;//节点
string xmlPath;//xml文件地址
bool returnValue;
returnValue = true;
xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";//获取地址
xmlDocument = new XmlDocument();
try
{
if (File.Exists(xmlPath))//xml文件是否存在
{
xmlDocument.Load(xmlPath);//加载xml文件
rootNode = xmlDocument.DocumentElement;//获取xml根节点
subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
xmlElement = xmlDocument.CreateElement(tableName);//创建一个节点(例如:T_TMENUGROUPMAIN)
xmlElement.SetAttribute(columnCode, code);//添加节点属性(例如:F_MENUGROUPCODE="1")
xmlElement.SetAttribute(columnName, name);//添加节点属性(例如:F_MENUGROUPNAME="123")
secondarySubNode.AppendChild(xmlElement);//将创建的节点T_TMENUGROUPMAIN添加到MENUGROUPMAIN节点里面
xmlDocument.Save(xmlPath);//保存xml文件
}
else
{
returnValue = false;
return returnValue;
}
}
catch
{
returnValue = false;
}
return returnValue;
}
//修改
public bool UpdateConfigSettings(string nodeName, string tableName, string columnCode, string columnName, string name, string condition)
{
XmlDocument xmlDocument;//xml文件
XmlElement rootNode;//根节点
XmlNode subNode;//子节点
XmlNode secondarySubNode;//次级子节点
XmlNodeList nodes;//节点集合
string xmlPath;//xml文件地址
bool returnValue;
returnValue = true;
xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
xmlDocument = new XmlDocument();
try
{
if (File.Exists(xmlPath))//xml文件是否存在
{
xmlDocument.Load(xmlPath);//加载xml文件
rootNode = xmlDocument.DocumentElement;//获取xml根节点
subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
nodes = secondarySubNode.SelectNodes(tableName);//查询MENUGROUPMAIN中的所有节点
foreach (XmlNode node in nodes)//循环节点集合
{
if (node.Attributes[columnCode].Value == condition)//判断要修的属性
{
node.Attributes[columnName].Value = name;//修改属性
}
}
xmlDocument.Save(xmlPath);//保存xml文件
}
else
{
returnValue = false;
return returnValue;
}
}
catch
{
returnValue = false;
}
return returnValue;
}
//删除
public bool DeleteConfigSettings(string nodeName, string tableName, string columnCode, string condition)
{
XmlDocument xmlDocument;//xml文件
XmlElement rootNode;//根节点
XmlNode subNode;//子节点
XmlNode secondarySubNode;//次级子节点
XmlNodeList nodes;//节点集合
string xmlPath;//xml文件地址
bool returnValue;
returnValue = true;
xmlPath = AppDomain.CurrentDomain.BaseDirectory.ToString() + "TMESMaintenance.exe.config";
xmlDocument = new XmlDocument();
try
{
if (File.Exists(xmlPath))//xml文件是否存在
{
xmlDocument.Load(xmlPath);//加载xml文件
rootNode = xmlDocument.DocumentElement;//获取xml根节点
subNode = rootNode.SelectSingleNode("systemINFO");//查询根节点中的-systemINFO
secondarySubNode = subNode.SelectSingleNode(nodeName);//查询节点systemINFO中的-节点(例如:MENUGROUPMAIN)
nodes = secondarySubNode.SelectNodes(tableName);//查询MENUGROUPMAIN中的所有节点
for (int i = 0; i < nodes.Count; i++) //循环节点集合
{
if (nodes[i].Attributes[columnCode].Value == condition) //判断要删除的属性
{
nodes[i].ParentNode.RemoveChild(nodes[i]);//删除节点
}
}
xmlDocument.Save(xmlPath);//保存xml文件
}
else
{
returnValue = false;
return returnValue;
}
}
catch
{
returnValue = false;
}
return returnValue;
}