保存配置,获取配置,XML

本文介绍了一个使用C#进行XML配置文件读写的示例。通过创建和操作XmlDocument对象,实现配置信息的保存与加载。具体包括:定义配置项、创建XML文档、保存配置至文件、从文件加载配置、解析并应用配置。此方法适用于需要持久化配置信息的场景。

private void SaveSOConfig()
{
//保存配置
Dictionary<string, string> PrintConfigDIC = new Dictionary<string, string>();
PrintConfigDIC.Add("TIME1", dtTime1.Text);
PrintConfigDIC.Add("TIME2", dtTime2.Text);
PrintConfigDIC.Add("STARTUP1", ConvertUtility.ObjectToString(chkStartUp1.Checked));
PrintConfigDIC.Add("STARTUP2", ConvertUtility.ObjectToString(chkStartUp2.Checked));
try
{
string path = @"C:\LayoutStyle\StandingOrderConfig.xml";
if (File.Exists(path))
{
File.Delete(path);
}

XmlDocument xml = new XmlDocument();//创建根节点 config
xml.AppendChild(xml.CreateXmlDeclaration("1.0", "utf-8", ""));
XmlElement one = xml.CreateElement("StandingOrderConfig");//把根节点加到xml文档中
foreach (var pc in PrintConfigDIC)
{
one.SetAttribute(pc.Key, pc.Value);
}
xml.AppendChild(one);
xml.Save(path);

MessageBox.Show("配置保存成功", "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);

LoadGridView();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "提示信息", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
}
}

private void LoadSOConfig()
{
//获取配置
string path = @"C:\LayoutStyle\StandingOrderConfig.xml";
if (File.Exists(path))
{
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlNodeList nodeList = doc.SelectNodes("/StandingOrderConfig");
if (nodeList != null)
{
foreach (XmlNode node in nodeList)
{
dtTime1.EditValue = node.Attributes["TIME1"].Value;
dtTime2.EditValue = node.Attributes["TIME2"].Value;
chkStartUp1.Checked = ConvertUtility.ObjectTobool(node.Attributes["STARTUP1"].Value);
chkStartUp2.Checked = ConvertUtility.ObjectTobool(node.Attributes["STARTUP2"].Value);

DDDs.Clear();
if (chkStartUp1.Checked)
{
DDDs.Add(new DDD() { DateTimeOfExecOnce = node.Attributes["TIME1"].Value, difftime = "24H", IsRunning = "正在执行...", jobname = "长期医嘱更新" });
}
if (chkStartUp2.Checked)
{
DDDs.Add(new DDD() { DateTimeOfExecOnce = node.Attributes["TIME2"].Value, difftime = "24H", IsRunning = "正在执行...", jobname = "长期医嘱床位费更新" });
}
break;
}
}
}
}

转载于:https://www.cnblogs.com/mapstar/p/10774581.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值