//public void ReadXml()
//{
// XmlTextReader reader = new XmlTextReader(txtOriResource.Text);
// while (reader.Read())
// {
// //判断当前读取得节点类型
// switch (reader.NodeType)
// {
// case XmlNodeType.Element:
// if (reader.Name == "appSettings")
// {
// while (reader.Read())
// {
// if (reader.Name == "system.web")
// break;
// if (reader.HasAttributes)
// {
// reader.MoveToAttribute(0);
// string key = reader.Value;
// reader.MoveToAttribute(1);
// string value = reader.Value;
// dic.Add(key, value);
// }
// }
// }
// break;
// }
// }
//}
public void ReadXml()
{
XmlDocument doc = new XmlDocument();
doc.Load(txtOriResource.Text);
XmlNodeList nodes = doc.SelectNodes("configuration/appSettings/add");
foreach (XmlNode node in nodes)
{
dic.Add(node.Attributes["key"].Value, node.Attributes["value"].Value);
}
}
/// <summary>
/// 更新xml文件
/// </summary>
public void WriteXml()
{
if (dic.Count > 0)
{
XmlDocument doc = new XmlDocument();
doc.Load(txtCurResource.Text);
XmlNodeList nodes = doc.SelectNodes("root/data");
foreach (var key in dic.Keys)//遍历字典,更新节点
{
XmlNode findNode = null;
foreach (XmlNode node in nodes)
{
if (node.Attributes["name"].Value == key)
{
findNode = node;
break;
}
}
if (findNode != null) //如果找到节点,便进行更新,否则就添加节点
{
findNode.InnerText = dic[key];
}
else
{
//XmlNode root = doc.SelectSingleNode("root"); //查找根节点
//XmlElement ele = doc.CreateElement("data"); //创建一个节点
//ele.SetAttribute("name", key);
//ele.SetAttribute("xml:space", "preserve"); //添加节点的属性
//XmlElement eleSub = doc.CreateElement("value"); //创建一个子节点
//eleSub.InnerText = dic[key];
//ele.AppendChild(eleSub);
//root.AppendChild(ele); //把节点附加到根节点
}
}
DialogResult result= fbd.ShowDialog();
if (result == DialogResult.OK) //如果确定保存,选择保存路径
{
string suf = txtCurResource.Text.Substring(txtCurResource.Text.LastIndexOf("\\"));
string path=fbd.SelectedPath+suf;
doc.Save(path);
lblStatus.Text = "文件已更新至:"+path;
}
}
}
//{
// XmlTextReader reader = new XmlTextReader(txtOriResource.Text);
// while (reader.Read())
// {
// //判断当前读取得节点类型
// switch (reader.NodeType)
// {
// case XmlNodeType.Element:
// if (reader.Name == "appSettings")
// {
// while (reader.Read())
// {
// if (reader.Name == "system.web")
// break;
// if (reader.HasAttributes)
// {
// reader.MoveToAttribute(0);
// string key = reader.Value;
// reader.MoveToAttribute(1);
// string value = reader.Value;
// dic.Add(key, value);
// }
// }
// }
// break;
// }
// }
//}
public void ReadXml()
{
XmlDocument doc = new XmlDocument();
doc.Load(txtOriResource.Text);
XmlNodeList nodes = doc.SelectNodes("configuration/appSettings/add");
foreach (XmlNode node in nodes)
{
dic.Add(node.Attributes["key"].Value, node.Attributes["value"].Value);
}
}
/// <summary>
/// 更新xml文件
/// </summary>
public void WriteXml()
{
if (dic.Count > 0)
{
XmlDocument doc = new XmlDocument();
doc.Load(txtCurResource.Text);
XmlNodeList nodes = doc.SelectNodes("root/data");
foreach (var key in dic.Keys)//遍历字典,更新节点
{
XmlNode findNode = null;
foreach (XmlNode node in nodes)
{
if (node.Attributes["name"].Value == key)
{
findNode = node;
break;
}
}
if (findNode != null) //如果找到节点,便进行更新,否则就添加节点
{
findNode.InnerText = dic[key];
}
else
{
//XmlNode root = doc.SelectSingleNode("root"); //查找根节点
//XmlElement ele = doc.CreateElement("data"); //创建一个节点
//ele.SetAttribute("name", key);
//ele.SetAttribute("xml:space", "preserve"); //添加节点的属性
//XmlElement eleSub = doc.CreateElement("value"); //创建一个子节点
//eleSub.InnerText = dic[key];
//ele.AppendChild(eleSub);
//root.AppendChild(ele); //把节点附加到根节点
}
}
DialogResult result= fbd.ShowDialog();
if (result == DialogResult.OK) //如果确定保存,选择保存路径
{
string suf = txtCurResource.Text.Substring(txtCurResource.Text.LastIndexOf("\\"));
string path=fbd.SelectedPath+suf;
doc.Save(path);
lblStatus.Text = "文件已更新至:"+path;
}
}
}