對於直接建立一個xml文件這個,直接略過。假設xml的初始內容以下面:node
<?xml version="1.0"?> <Conf> </Conf>
哪,根節點是Conf。網站
public static bool AddConfig(CommonConfig conf) { try { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlElement root = doc.DocumentElement; XmlElement body = doc.CreateElement("Config"); XmlElement CommonContent = doc.CreateElement("CommonContent"); CommonContent.InnerXml = "<![CDATA[" + conf.CommonContent + "]]>"; XmlElement CommonPriority = doc.CreateElement("CommonPriority"); CommonPriority.InnerText = Priority.ToString(); body.AppendChild(CommonContent); body.AppendChild(CommonPriority); XmlAttribute Attr = doc.CreateAttribute("CommonName"); Attr.InnerText = conf.CommonName; body.Attributes.Append(Attr); root.AppendChild(body); doc.Save(ConfigPath); return true; } catch (Exception ex) { LogMsg.WriteLog("通用配置添加配置信息異常:" + ex.ToString()); return false; } }
public static bool ModifyConfig(CommonConfig conf) { try { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlElement root = doc.DocumentElement; XmlNode CommonContentNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + conf.CommonName + "']/CommonContent"); CommonContentNode.InnerXml = "<![CDATA[" + conf.CommonContent + "]]>"; ; //由於是用屬性CommonName來肯定節點的,因此將修改的操做放在最後,不然對於子節點將獲取不到 XmlNode confNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + conf.CommonName + "']"); confNode.Attributes["CommonName"].Value = conf.NewCommonName; doc.Save(ConfigPath); return true; } catch (Exception ex) { LogMsg.WriteLog("通用配置修改配置信息異常:" + ex.ToString()); return false; } }
public static List<CommonConfig> GetAllConfig() { List<CommonConfig> ListConf = new List<CommonConfig>(); string CacheKey = "CommonConfig_Cache"; System.Web.Caching.Cache objCache = HttpRuntime.Cache; if (objCache[CacheKey] == null) { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlElement root = doc.DocumentElement; XmlNodeList nodeList=root.ChildNodes; foreach (XmlNode curNode in nodeList) { CommonConfig conf = new CommonConfig(); conf.CommonName = curNode.Attributes["CommonName"].Value; XmlNode contentNode=curNode.SelectSingleNode("CommonContent"); conf.CommonContent = contentNode.InnerText; XmlNode PriorityNode = curNode.SelectSingleNode("CommonPriority"); conf.CommonPriority = Convert.ToInt32(PriorityNode.InnerText); ListConf.Add(conf); } System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(ConfigPath); DataCache.SetCache(CacheKey, ListConf, dep); return ListConf; } else { return objCache[CacheKey] as List<CommonConfig>; } }
public static CommonConfig GetConfigByName(string CommonName) { try { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlElement root = doc.DocumentElement; XmlNode ConfigNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + CommonName + "']"); CommonConfig conf = new CommonConfig(); conf.CommonName = ConfigNode.Attributes["CommonName"].Value; XmlNode contentNode = ConfigNode.SelectSingleNode("CommonContent"); conf.CommonContent = contentNode.InnerText; XmlNode PriorityNode = ConfigNode.SelectSingleNode("CommonPriority"); conf.CommonPriority = Convert.ToInt32(PriorityNode.InnerText); return conf; } catch (Exception ex) { LogMsg.WriteLog("通用配置添加配置信息異常:" + ex.ToString()); return null; } }
public static bool DeleteConfig(string Name) { try { XmlDocument doc = new XmlDocument(); doc.Load(ConfigPath); XmlElement root = doc.DocumentElement; XmlNode conf = root.SelectSingleNode("/Conf/Config[@CommonName='" + Name + "']"); root.RemoveChild(conf); doc.Save(ConfigPath); return true; } catch (Exception ex) { LogMsg.WriteLog("通用配置刪除配置信息異常:" + ex.ToString()); return false; } }
最終的結果是這樣的: spa
<?xml version="1.0"?> <Conf> <Config CommonName="網站Logo"> <CommonContent><![CDATA[這是什麼呢,你告訴我]]></CommonContent> <CommonPriority>1</CommonPriority> </Config> </Conf>
上面須要使用XPath語法,我也記不住,因此放一個連接在這裏,之後用到就去看看就好了。
而<![CDATA[]]>則能夠存儲特殊字符,而不會干擾xml的結構。
innerText和innerHtml的區別是:
innerText:這是什麼呢,你告訴我
innerHtml:<![CDATA[這是什麼呢,你告訴我]]>
code
可能之後還要添加一些對Xml的操做,未完待續……xml