For directly creating an XML file this, skip directly. Assume that the initial content of the XML is the following side:
<? XML version= "1.0" ?> < Conf > </ Conf >
Where the root node is CONF.
Adding nodes
Public Static BOOLaddconfig (Commonconfig conf) {Try{XmlDocument doc=NewXmlDocument (); Doc. Load (Configpath); XmlElement Root=Doc. DocumentElement; XmlElement Body= Doc. CreateElement ("Config"); XmlElement commoncontent= Doc. CreateElement ("commoncontent"); Commoncontent.innertext=conf.commoncontent; XmlElement commonpriority= Doc. CreateElement ("commonpriority"); Commonpriority.innertext=conf.CommonPriority.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 ("General Configuration Add configuration information exception:"+Ex. ToString ()); return false; } } #endregion
Edit Node
Public Static BOOLmodifyconfig (Commonconfig conf) {Try{XmlDocument doc=NewXmlDocument (); Doc. Load (Configpath); XmlElement Root=Doc. DocumentElement; XmlNode Commoncontentnode= root. selectSingleNode ("/conf/config[@CommonName = '"+ Conf.commonname +"']/commoncontent"); Commoncontentnode.innertext=conf.commoncontent; //because the attribute commonname is used to determine the node, the modified action is placed at the end, otherwise the child node will not getXmlNode Confnode = root. selectSingleNode ("/conf/config[@CommonName = '"+ Conf.commonname +"']"); confnode.attributes["CommonName"]. Value =Conf. Newcommonname; Doc. Save (Configpath); return true; } Catch(Exception ex) {Logmsg.writelog ("Common Configuration Modify configuration information exception:"+Ex. ToString ()); return false; } }
Get all nodes
Public StaticList<commonconfig>Getallconfig () {List<CommonConfig> listconf =NewList<commonconfig>(); stringCacheKey ="Commonconfig_cache"; System.Web.Caching.Cache Objcache=Httpruntime.cache; if(Objcache[cachekey] = =NULL) {XmlDocument doc=NewXmlDocument (); Doc. Load (Configpath); XmlElement Root=Doc. DocumentElement; XmlNodeList NodeList=Root. ChildNodes; foreach(XmlNode CurnodeinchnodeList) {Commonconfig conf=NewCommonconfig (); 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=NewSystem.Web.Caching.CacheDependency (Configpath); Datacache.setcache (CacheKey, listconf, DEP); returnlistconf; } Else { returnObjcache[cachekey] asList<commonconfig>; } }
The end result is this:
<?XML version= "1.0"?><Conf> <ConfigCommonName= "website logo"> <commoncontent>Website logo</commoncontent> <commonpriority>1</commonpriority> </Config></Conf>
The above need to use the XPath syntax, I also can not remember, so put a link here, later used to go to see on the line.
You might want to add some operations to the XML later ...
. NET general operations for XML