XML文件内容如下:site.config
Code
<?xml version="1.0" encoding="utf-8" ?>
<sites>
<site siteid="1" securitylevel="" publickey="" publicandprivatekey="" fromurlkey="" cryptkeyfield="" ivkeyfield="">
</site>
<site siteid="2" securitylevel="" publickey="" publicandprivatekey="" fromurlkey="" cryptkeyfield="" ivkeyfield="">
</site>
</sites>
读取XML
Code
string cfgPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sites.config");
XmlDocument doc = new XmlDocument();
doc.Load(cfgPath);
XmlElement root = doc.DocumentElement;//:获取文档的根 System.Xml.XmlElement。
XmlNode node = root.SelectSingleNode(string.Format("site[@siteid='{0}']", siteID));// 选择匹配 XPath 表达式的第一个 XmlNode。
XmlAttribute att = node.Attributes["publickey"];
if (att != null)
{
_publicKey = att.Value;
}
写入XML
Code
string cfgPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "sites.config");
XmlDocument doc = new XmlDocument();
doc.Load(cfgPath);
XmlElement root = doc.DocumentElement;
XmlNode node = doc.CreateElement("site");//创建创建具有指定名称的元素
XmlAttribute att = doc.CreateAttribute("siteid"); //创建属性
att.Value = _siteID;
node.Attributes.Append(att);
root.AppendChild(node);
doc.Save(cfgPath);