.net对于Xml的常规操做

对于直接建立一个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

相关文章
相关标签/搜索