📄 manageconfigs.cs
字号:
using System;
using System.Xml;
using System.IO;
namespace DS.EMIS.StartPrepare.Common
{
/// <summary>
///
/// </summary>
internal class ManageConfigs
{
public ManageConfigs()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 标准配置文件的读写
/*
* <?xml version="1.0" encoding="utf-8"?>
* <configuration>
* <appSettings>
* <add key="名称1" value="值" />
* <add key="名称2" value="值" />
* </appSettings>
* </configuration>
*/
public string GetConfig(string KeyValue,string FileName)
{
try
{
string reValue = "";
string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
if(System.IO.Path.IsPathRooted(FileName))
{
doc.Load(FileName);
}
else
{
doc.Load(currAppPath+".\\Configs\\"+FileName);
}
System.Xml.XmlNode node = doc.SelectSingleNode("//add[@key='"+KeyValue+"']");
System.Xml.XmlElement ele = (System.Xml.XmlElement)node;
reValue = ele.GetAttribute("value");
return reValue;
}
catch
{
return String.Empty;
}
}
public void UpdateConfig(string KeyValue,string Xvalue,string FileName)
{
string currAppPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(currAppPath+".\\Configs\\"+FileName);
System.Xml.XmlNode node = doc.SelectSingleNode(@"//add[@key='"+KeyValue+"']");
System.Xml.XmlElement ele = (System.Xml.XmlElement)node;
ele.SetAttribute("value",Xvalue);
doc.Save(currAppPath+".\\Configs\\"+FileName);
}
/*
* <?xml version="1.0" standalone="yes"?>
* <configuration>
* <接点名称 属性1="值" 属性2="值" 属性3="值" ...../>
*例:<PartnerAgent Name="DS.EMIS.AppIntegrate.EMISCenterTerminalAgent" Type="DS.EMIS.AppIntegrate.EMISCenterTerminalAgent" InternalType="102" />
* </configuration>
*/
public System.Data.DataTable GetConfigs(string fileName,string nodeName)
{
System.Data.DataTable dataTable = new System.Data.DataTable("Configs");
XmlDocument doc = new XmlDocument();
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Load(fileName);
}
else
{
doc.Load(".\\Configs\\"+fileName);
}
foreach(XmlNode node in doc.DocumentElement.ChildNodes)
{
if(node.Name.ToUpper() == nodeName.ToUpper())
{
if(dataTable.Columns.Count == 0)
{
for(int i=0;i<node.Attributes.Count;i++)
{
dataTable.Columns.Add(node.Attributes[i].Name,typeof(System.String));
}
}
System.Data.DataRow dr = dataTable.NewRow();
for(int i=0;i<node.Attributes.Count;i++)
{
try
{
dr[node.Attributes[i].Name] = node.Attributes[i].Value.ToString();
}
catch
{
}
}
dataTable.Rows.Add(dr);
}
}
dataTable.AcceptChanges();
return dataTable;
}
public void UpdateConfig(string fileName,string nodeName,string attriName,string attriValue,int pos)
{
XmlDocument doc = new XmlDocument();
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Load(fileName);
}
else
{
doc.Load(".\\Configs\\"+fileName);
}
for(int i=0;i< doc.DocumentElement.ChildNodes.Count; i++ )
{
if(i == pos)
{
XmlNode node = doc.DocumentElement.ChildNodes[i];
if(node.Name.ToUpper() == nodeName)
{
for(int j=0;j<node.Attributes.Count;j++)
{
if(node.Attributes[j].Name == attriName)
{
node.Attributes[j].Value = attriValue;
break;
}
}
}
}
}
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Save(fileName);
}
else
{
doc.Save(".\\Configs\\"+fileName);
}
}
public void UpdateConfig(string fileName,string nodeName,string attriName,string attriValue,string matchAttriName,string matchAttriValue)
{
XmlDocument doc = new XmlDocument();
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Load(fileName);
}
else
{
doc.Load(".\\Configs\\"+fileName);
}
System.Xml.XmlNodeList nodes = doc.SelectNodes(@"//"+nodeName+"[@"+matchAttriName+"='"+matchAttriValue+"']");
foreach(XmlNode node in nodes)
{
for(int i=0;i<node.Attributes.Count;i++)
{
if(node.Attributes[i].Name == attriName)
{
node.Attributes[i].Value = attriValue;
break;
}
}
}
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Save(fileName);
}
else
{
doc.Save(".\\Configs\\"+fileName);
}
}
public void InsertNode(string fileName,string MainNode,string Element,string[] Attrib,string[] AttribContent,string Content)
{
XmlDocument doc = new XmlDocument();
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Load(fileName);
}
else
{
doc.Load(".\\Configs\\"+fileName);
}
XmlNode objNode = doc.SelectSingleNode(MainNode);
XmlElement objElement = doc.CreateElement(Element);
for(int i = 0; i < Attrib.Length; i++)
{
objElement.SetAttribute(Attrib[i],AttribContent[i]);
}
objElement.InnerText = Content;
objNode.AppendChild(objElement);
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Save(fileName);
}
else
{
doc.Save(".\\Configs\\"+fileName);
}
}
//此方法有待改进
public void DeleteNode(string fileName,string nodeName,string[] Attrib,string[] AttribContent)
{
XmlDocument doc = new XmlDocument();
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Load(fileName);
}
else
{
doc.Load(".\\Configs\\"+fileName);
}
string mainNode = nodeName.Substring(0, nodeName.LastIndexOf("/"));
XmlNode fNode = doc.SelectSingleNode(mainNode);
XmlNode zNode = doc.SelectSingleNode(nodeName);
fNode.RemoveChild(zNode);
if(System.IO.Path.IsPathRooted(fileName))
{
doc.Save(fileName);
}
else
{
doc.Save(".\\Configs\\"+fileName);
}
// //xmlTool.Delete("Book/Author[BID=\"4\"]");
//
// XmlDocument doc = new XmlDocument();
// doc.Load(".\\Configs\\"+fileName);
//
// string strXPath = "//"+nodeName+"[@";
//
// for(int i = 0; i < Attrib.Length; i++)
// {
// strXPath += Attrib[i] + "='" +AttribContent[i]+"'";
// }
//
// //doc.SelectSingleNode(mainNode).RemoveChild(objXmlDoc.SelectSingleNode(Node));
// string ss = "//"+nodeName+"[@"+matchAttriName+"='"+matchAttriValue+"']";
//
}
#endregion
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -