.net对于Xml的常规操作

对于直接创建一个xml文件这个,直接略过。假设xml的初始内容如下面:

<?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;
            }
        }

最终的结果是这样的: 

<?xml version="1.0"?>
<Conf>
  <Config CommonName="网站Logo">
    <CommonContent><![CDATA[这是什么呢,你告诉我]]></CommonContent>
    <CommonPriority>1</CommonPriority>
  </Config>
</Conf>

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

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

原文地址:https://www.cnblogs.com/hougelou/p/4930580.html