.net对于Xml的常规操作

前端之家收集整理的这篇文章主要介绍了.net对于Xml的常规操作前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
对于直接创建一个xml文件这个,直接略过。假设xml的初始内容如下面:
  1. <?xml version="1.0"?>
  2. <Conf>
  3. </Conf>

哪,根节点是Conf。

添加节点
  1. public static bool AddConfig(CommonConfig conf)
  2. {
  3. try
  4. {
  5. XmlDocument doc = new XmlDocument();
  6. doc.Load(ConfigPath);
  7. XmlElement root = doc.DocumentElement;
  8. XmlElement body = doc.CreateElement("Config");
  9. XmlElement CommonContent = doc.CreateElement("CommonContent");
  10. CommonContent.InnerXml = "<![CDATA[" + conf.CommonContent + "]]>";
  11. XmlElement CommonPriority = doc.CreateElement("CommonPriority");
  12. CommonPriority.InnerText = Priority.ToString();
  13. body.AppendChild(CommonContent);
  14. body.AppendChild(CommonPriority);
  15. XmlAttribute Attr = doc.CreateAttribute("CommonName");
  16. Attr.InnerText = conf.CommonName;
  17. body.Attributes.Append(Attr);
  18. root.AppendChild(body);
  19. doc.Save(ConfigPath);
  20. return true;
  21. }
  22. catch (Exception ex)
  23. {
  24. LogMsg.WriteLog("通用配置添加配置信息异常:" + ex.ToString());
  25. return false;
  26. }
  27. }
编辑节点
  1. public static bool ModifyConfig(CommonConfig conf)
  2. {
  3. try
  4. {
  5. XmlDocument doc = new XmlDocument();
  6. doc.Load(ConfigPath);
  7. XmlElement root = doc.DocumentElement;
  8. XmlNode CommonContentNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + conf.CommonName + "']/CommonContent");
  9. CommonContentNode.InnerXml = "<![CDATA[" + conf.CommonContent + "]]>"; ;
  10. //因为是用属性CommonName来确定节点的,所以将修改的操作放在最后,否则对于子节点将获取不到
  11. XmlNode confNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + conf.CommonName + "']");
  12. confNode.Attributes["CommonName"].Value = conf.NewCommonName;
  13. doc.Save(ConfigPath);
  14. return true;
  15. }
  16. catch (Exception ex)
  17. {
  18. LogMsg.WriteLog("通用配置修改配置信息异常:" + ex.ToString());
  19. return false;
  20. }
  21. }
获取所有节点
  1. public static List<CommonConfig> GetAllConfig()
  2. {
  3. List<CommonConfig> ListConf = new List<CommonConfig>();
  4. string CacheKey = "CommonConfig_Cache";
  5. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  6. if (objCache[CacheKey] == null)
  7. {
  8. XmlDocument doc = new XmlDocument();
  9. doc.Load(ConfigPath);
  10. XmlElement root = doc.DocumentElement;
  11. XmlNodeList nodeList=root.ChildNodes;
  12. foreach (XmlNode curNode in nodeList)
  13. {
  14. CommonConfig conf = new CommonConfig();
  15. conf.CommonName = curNode.Attributes["CommonName"].Value;
  16. XmlNode contentNode=curNode.SelectSingleNode("CommonContent");
  17. conf.CommonContent = contentNode.InnerText;
  18. XmlNode PriorityNode = curNode.SelectSingleNode("CommonPriority");
  19. conf.CommonPriority = Convert.ToInt32(PriorityNode.InnerText);
  20. ListConf.Add(conf);
  21. }
  22. System.Web.Caching.CacheDependency dep = new System.Web.Caching.CacheDependency(ConfigPath);
  23. DataCache.SetCache(CacheKey,ListConf,dep);
  24. return ListConf;
  25. }
  26. else
  27. {
  28. return objCache[CacheKey] as List<CommonConfig>;
  29. }
  30. }
获取某个节点信息
  1. public static CommonConfig GetConfigByName(string CommonName)
  2. {
  3. try
  4. {
  5. XmlDocument doc = new XmlDocument();
  6. doc.Load(ConfigPath);
  7. XmlElement root = doc.DocumentElement;
  8. XmlNode ConfigNode = root.SelectSingleNode("/Conf/Config[@CommonName='" + CommonName + "']");
  9. CommonConfig conf = new CommonConfig();
  10. conf.CommonName = ConfigNode.Attributes["CommonName"].Value;
  11. XmlNode contentNode = ConfigNode.SelectSingleNode("CommonContent");
  12. conf.CommonContent = contentNode.InnerText;
  13. XmlNode PriorityNode = ConfigNode.SelectSingleNode("CommonPriority");
  14. conf.CommonPriority = Convert.ToInt32(PriorityNode.InnerText);
  15. return conf;
  16. }
  17. catch (Exception ex)
  18. {
  19. LogMsg.WriteLog("通用配置添加配置信息异常:" + ex.ToString());
  20. return null;
  21. }
  22. }
删除节点
  1. public static bool DeleteConfig(string Name)
  2. {
  3. try
  4. {
  5. XmlDocument doc = new XmlDocument();
  6. doc.Load(ConfigPath);
  7. XmlElement root = doc.DocumentElement;
  8. XmlNode conf = root.SelectSingleNode("/Conf/Config[@CommonName='" + Name + "']");
  9. root.RemoveChild(conf);
  10. doc.Save(ConfigPath);
  11. return true;
  12. }
  13. catch (Exception ex)
  14. {
  15. LogMsg.WriteLog("通用配置删除配置信息异常:" + ex.ToString());
  16. return false;
  17. }
  18. }

最终的结果是这样的:

  1. <?xml version="1.0"?>
  2. <Conf>
  3. <Config @H_404_498@CommonName="网站logo">
  4. <CommonContent><![CDATA[这是什么呢,你告诉我]]></CommonContent>
  5. <CommonPriority>1</CommonPriority>
  6. </Config>
  7. </Conf>

上面需要使用XPath语法,我也记不住,所以放一个链接这里,以后用到就去看看就行了。
而<![CDATA[]]>则可以存储特殊字符,而不会干扰xml的结构。
innerText和innerHtml的区别是:
innerText:@H_404_498@这是什么呢,你告诉我
innerHtml:@H_404_498@<![CDATA[这是什么呢,你告诉我]]>

可能以后还要添加一些对Xml的操作,未完待续……

猜你在找的XML相关文章