📄 operaxml.cs
字号:
#region 严重声明
//此程序开源,仅为经验交流用。
//请勿做出违法的程序
//如有做出违法行为,本人不负责任何法律责任。
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace board.GetXml
{
class OperaXml
{
public List<attribute> ReadXml(string name)
{
string filePath = System.IO.Path.GetFullPath("config.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode xn = xmlDoc.SelectSingleNode("Configuration");
XmlNodeList xnl = xn.SelectNodes("Node[@Name='" + name + "']");
List<attribute> list = new List<attribute>();
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
attribute attr = new attribute();
attr.Name = xn2.Name;
attr.Value = xn2.InnerText;
list.Add(attr);
}
}
return list;
}
public List<string> ReadNodeXml()
{
string filePath = System.IO.Path.GetFullPath("config.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode xn = xmlDoc.SelectSingleNode("Configuration");
XmlNodeList xnl = xn.ChildNodes;
List<string> list = new List<string>();
foreach (XmlNode xnf in xnl)
{
XmlElement xe = (XmlElement)xnf;
list.Add(xe.GetAttribute("Name"));
}
return list;
}
public void WriteXml(string name,List<attribute> list) {
string filePath = System.IO.Path.GetFullPath("config.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("Configuration");
XmlElement xe1 = xmlDoc.CreateElement("Node");
xe1.SetAttribute("Name", name);
foreach (attribute _attribute in list) {
XmlElement xesub1 = xmlDoc.CreateElement(_attribute.Name);
xesub1.InnerText = _attribute.Value;
xe1.AppendChild(xesub1);
}
root.AppendChild(xe1);
xmlDoc.Save(filePath);
}
public void EditXml(string name, List<attribute> list){
string filePath = System.IO.Path.GetFullPath("config.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("Configuration");
XmlNodeList nodeList = root.SelectNodes("Node[@Name='" + name + "']");
foreach (XmlNode xn in nodeList)
{
foreach (attribute _attributer in list)
{
XmlElement xe = (XmlElement)xn;
XmlNodeList xnf1 = xe.ChildNodes;
foreach (XmlNode xn2 in xnf1)
{
if (xn2.Name == _attributer.Name)
{
xn2.InnerText = _attributer.Value;
}
}
}
}
xmlDoc.Save(filePath);
}
public void DeleteXml(string name) {
string filePath = System.IO.Path.GetFullPath("config.xml");
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(filePath);
XmlNode root = xmlDoc.SelectSingleNode("Configuration");
XmlNodeList nodeList = root.SelectNodes("Node[@Name='" + name + "']");
foreach (XmlNode xn in nodeList)
{
root.RemoveChild(xn);
}
xmlDoc.Save(filePath);
}
}
class attribute{
private string _Name;
private string _Value;
public string Name{get{return _Name;}set{_Name = value;}}
public string Value{get{return _Value;}set{_Value = value;}}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -